using GSON to parse and place into a list of objects

ε祈祈猫儿з 提交于 2019-12-23 09:27:28

问题


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"


回答1:


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".



来源:https://stackoverflow.com/questions/7924189/using-gson-to-parse-and-place-into-a-list-of-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!