Home » Android » android – using GSON to parse and place into a list of objects

android – using GSON to parse and place into a list of objects

Posted by: admin June 15, 2020 Leave a comment

Questions:

I found this article: http://www.dev-articles.com/article/Google-gson-and-list-of-objects-386003

It looks like it is trying to do what I want, which is parse my json and place them into a List of objects.

What I dont get, or maybe the article is missing something, is how the “Project” class gets used. It seems it comes out of nowhere.

EDIT:

Thanks to yorkw I now have:

public static String parseJSONResponse(String jsonResponse) {

        Type listType = new TypeToken<List<SingleEvent>>(){}.getType();
        List<SingleEvent> events = (List<SingleEvent>) Gson.fromJson(jsonResponse, listType);


}

However im getting red flags over Type.”Type cannot be resolved to a type”

How to&Answers:

Project is the domain model you need implemented for deserializing json string:

Type listType = new TypeToken<List<Project>>(){}.getType();
List<Project> projects = (List<Project>) gson.fromJson(response, listType);

You can use generic type List as described in that article, in Eclipse, you get a yellow warning mark “List is a raw type. References to generic type List should be parameterized”.