gpt4 book ai didi

java - jackson 反序列化为没有注释的POJO

转载 作者:行者123 更新时间:2023-11-30 08:45:17 25 4
gpt4 key购买 nike

我生成了这个 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));

最佳答案

一种方法是使用 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

关于java - jackson 反序列化为没有注释的POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33367088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com