【问题标题】:How to mapper a tree json object using jackson to List<>如何使用杰克逊将树 json 对象映射到 List<>
【发布时间】:2018-10-01 20:52:29
【问题描述】:

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

【问题讨论】:

标签: java json hibernate


【解决方案1】:

如果您使用的是 Jackson,请尝试使用 JsonPropety 注释对字段(或 getter/setter)进行注释。

像这样:

@JsonProperty("device.id") private Device device;

【讨论】:

    【解决方案2】:

    所以,我在结果集上使用了一个新的转换器解决了这个问题。转换器实际上只是迭代结果并搜索“。”。使用反射。我想这是一个糟糕的策略。所以,我接受新的答案。

    我使用的变压器是:AliasToBeanNestedResultTransformer

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      相关资源
      最近更新 更多