How can I use GSON to parse and place into a list of objects?

前端 未结 1 1117
遇见更好的自我
遇见更好的自我 2020-12-20 16:26

I have a domain object Foo, and I want to parse some JSON such as

[
    {"prop": "val"},
    {"prop": "val2"},
]


        
相关标签:
1条回答
  • 2020-12-20 16:58

    You need to use a TypeToken to correctly express the type. Class is not sufficient in this case, because of the interaction with the generic type.

    Type listType = new TypeToken<List<Foo>>(){}.getType();
    List<Foo> projects = (List<Foo>) gson.fromJson(response, listType);
    
    0 讨论(0)
提交回复
热议问题