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

前端 未结 1 1118
遇见更好的自我
遇见更好的自我 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>(){}.getType();
    List projects = (List) gson.fromJson(response, listType);
    

    0 讨论(0)
提交回复
热议问题