如何使用com.fasterxml.jackson.databind.ObjectMapper
将这样的JSON映射到对象列表中:
{
"1": {
"id": 1,
"name": "Peter Parker",
"alias": "peter"
},
"2": {
"id": 2,
"name": "Berry Allen",
"alias": "berry"
},
"3": {
"id": 3,
"name": "Tony Stark",
"alias": "tony"
}
}
我特别挣扎着索引号码。如果没有这些连续的数字,我会做类似以下的事情,这是正常的'正常'对象数组很好。
ObjectMapper mapper = new ObjectMapper();
List<People> people = mapper.readValue(jsonString, new TypeReference<List<People>>(){});
我希望有人可以帮助我。提前谢谢。
答案 0 :(得分:0)
JSON结构代表地图,而不是列表:
ObjectMapper mapper = new ObjectMapper();
Map<String, People> peopleMap = mapper.readValue(jsonString, new TypeReference<Map<String, People>>(){});
Collection<People> allPeople = peopleMap.values();