我有一个JSONObject
响应,如
{
"0:"{
"name": "name1",
"surname": "surname1",
"id": "22",
"motivations": []
},
"1:"{
"name": "name2",
"surname": "surname2",
"id": "23",
"motivations": []
},
"2:"{
"name": "name3",
"surname": "surname3",
"id": "24",
"motivations": []
},
"sign": "9e46b7d6b140b",
"last_call": 1446
}
我想将它映射到List<Person>
,但在删除之前我无法执行此操作
"0:","1:","2:" ,"sign":"9e46b7d6b140b","last_call":1446
。
知道如何在模型中映射它吗?
答案 0 :(得分:1)
创建Person
类
class Person{
String name;
String surname;
String id;
String[] motivations;
//Create getter setter for it
}
创建HashMap<String,Object> map = new HashMap<>();
map = new Gson().fromJson(responseData, HashMap.class);
最后迭代地图
Iterator entries = map .entrySet().iterator();
while (entries.hasNext()) {
String value = (String )entries.next().getValue();
Object p;
try{
p = new Gson().fromJson(value , Person .class);
\\person class
}
catch(Exception e)
{
// catch exception
p =null;
}
if(p!= null && p instanceOf Person)
{
\\create list and add it to the separate list
\\ entries.next().getKey()
\\ entries.next().getValue()
}
}