我生成了这个json,我想反序列化为POJO列表
{
"members":
[
{"id":"947","route":"123","milkNo":"Z5623","nationalId":"65326812","firstName":"Server","otherName":"Farmer","country":"Kenya","county":"Nyandarua","address":"South Kinangop","phone1":"0752365258","phone2":"0712456321","email":"yunus.awse@gmail.com","sent":"0","remoteServer":1,"createdAt":"2015-10-21 11:17:31","updatedAt":"0000-00-00 00:00:00"}],
"milkPayment":
[
{"id":"3","memberNo":"1","quantity":"40","price":"36","amount":"1437.5","deductions":"16390","payable":"-14952.5","date":"2015-10-16","remoteServer":1,"createdAt":"2015-10-16 10:26:10"}
],
"advancePayment":[]
}
如何仅提取members
数组部分,以便我可以在以下代码中提供它以获取成员列表
ObjectMapper objectMapper = new ObjectMapper();
List<Member> newMemberDeductions = objectMapper.readValue(a, objectMapper.getTypeFactory().constructCollectionType(List.class, Member.class));
答案 0 :(得分:2)
使用JsonNode
(遍历json中所需的键)来实现此目的的一种方法。试试这个:
ObjectMapper objectMapper = new ObjectMapper();
JsonNode node = objectMapper.readTree(new File("D:/Tests/sample.txt"));
node = node.findValue("members");
List<Member> newMemberDeductions = objectMapper.readValue(node.toString(), objectMapper.getTypeFactory().constructCollectionType(List.class, Member.class));
在这里,我将您的json作为文件放在D:/Tests/sample.txt