如何使用杰克逊映射树 json 对象到列表<>



执行查询后,结果将转换为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"
}
]

我需要映射这个 json 以获取 List<> 对象,其中:

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 Device device;

因此,我在结果集上使用新的转换器解决了这种情况。转换器实际上只是迭代结果并搜索"."。使用反射。我想这是一个糟糕的策略。因此,我接受新的答案。

我使用的转换器是:AliasToBeanNestedResultTransformer

最新更新