问题
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