gpt4 book ai didi

java - 如何使用 jackson 将树 json 对象映射到 List<>

转载 作者:行者123 更新时间:2023-12-02 01:53:46 26 4
gpt4 key购买 nike

执行查询后,结果将转换为 JSON。格式如下:

[
{
"version": "1.0.1",
"device.id": 1234,
"user.id": 1234,
"device.platform": "IOS",
"lastActivity": null,
"id": 987,
"when": "2017-08-05",
"device.platformVersion": "1.2.2",
"endPointArn": "arn-here-123"
},
{
"version": "1.0.2",
"device.id": 2345,
"user.id": 9876,
"device.platform": "IOS",
"lastActivity": null,
"id": 753,
"when": "2017-08-05",
"device.platformVersion": "1.2.2",
"endPointArn": "arn-here-123"
}
]

我需要为 List<> 对象映射此 json,其中:

public class DeviceUser {
private Integer id;
private String version;
private Date when;
private String endPointArn;
private Device device;
private User user;
}

我们可以看到带有 attrObject.fieldObject 的键(例如:“device.id”:2345),但我看不到转换为预期格式的方法。最终的格式为:

[
{
"version": "1.0.1",
"user": {
"id": 1234
},
"device": {
"id": 1234,
"platform": "IOS",
"platformVersion": "1.2.2"
},
"lastActivity": null,
"id": 987,
"when": "2017-08-05",
"endPointArn": "arn-here-123"
},
{
"user": {
"id": 9876
},
"device": {
"id": 2345,
"platform": "IOS",
"platformVersion": "1.2.2"
},
"version": "1.0.2",
"lastActivity": null,
"id": 753,
"when": "2017-08-05",
"endPointArn": "arn-here-123"
}
]

最佳答案

如果您使用 Jackson,请尝试使用 JsonPropety 注释来注释字段(或 getter/setter)。

像这样:

@JsonProperty("device.id")
私有(private)设备;

关于java - 如何使用 jackson 将树 json 对象映射到 List<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52598741/

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