如何使用Jackson将JSON数组中的子文档表示为Java Collection? [英] How to represent sub-documents in JSON array as Java Collection using Jackson?

查看:60
本文介绍了如何使用Jackson将JSON数组中的子文档表示为Java Collection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果能知道最好的方法,可以最好地利用杰克逊注释将我们从Salesforce接收到的以下JSON响应反序列化为Java对象.

I would appreciate any help to know the best way to deserialize the following JSON response which we receive from Salesforce into a Java object using Jackson Annotations.

"records": [
  {
      "attributes": {
        "type": "Lead",
        "url": "/services/data/v30.0/sobjects/Lead/00Qi000000Jr44XEAR"
      },
      "Id": "00Qi000000Jr44XEAR",
      "Name": "Kristen Akin",
      "Address": {
          "city": null,
          "country": "USA",
          "state": "CA",
          "stateCode": null,
          "street": null
      },
      "Phone": "(434) 369-3100",
  },
  {
      "attributes": {
      "type": "Lead",
      "url": "/services/data/v30.0/sobjects/Lead/00Qi000000Jugv2EAB"
      },
      "Id": "00Qi000000Jugv2EAB",
      "Name": "Sarah Jones",
      "Address": {
        "city": null,
        "country": null,
        "state": "CA",
        "stateCode": null,
        "street": null
      },
      "Phone": "(408) 338-6066",
  }
]}

上面的JSON响应是一个包含2个元素的数组.我想用Jackson将此JSON结构表示为Java集合,就像这样:

The above JSON response is an array which contains 2 elements. I would like to represent this JSON structure as a Java Collection using Jackson, something like:

@JsonProperty("records")
ArrayList<LinkedHashMap<?, ?>> recordList

以上对象表示形式反序列化JSON响应,并在HashMap中表示为键-值"对,但问题是表示属性"和地址"子文档.在上面的HashMap中,它们的值表示为相应的JSON子文档,而我希望将Attributes子文档映射到Attribute对象,并且将Address子文档映射到HashMap中的Address对象,类似:

The above object representation deserializes the JSON response and represent as a Key-Value pair in HashMap but issue is representing "attributes" and "Address" subdocuments. In the above HashMap their value is being represented as the respective JSON subdocument whereas I would prefer to have Attributes subdocument gets mapped to an Attribute object and similarly Address subdocument mapped to an Address object in the HashMap, something like:

Key            Value
attributes     <Attributes> object
Id             00Qi000000Jr44XEAR
.....
Address        <Address> object
Phone          (434) 369-3100

在进行了一些Google搜索之后,我发现我可能必须使用@JsonTypeInfo和@JsonSubTypes属性,如本

After doing some Google search, I figured I might have to use @JsonTypeInfo and @JsonSubTypes attributes as mentioned in this link.

但是,我无法弄清楚在这种特定情况下如何使用这些注释.感谢任何帮助.

However, I could not figure how to use these annotations in this specific scenario. Appreciate any help on this.

推荐答案

如果JSON输入的结构是完全动态的,则可以将其读取为JsonNode甚至是Map.有关更多信息,请参考此链接.

If the structure of your JSON input is completely dynamic, you can read it as JsonNode or even as Map. Refer to this link for more info.

如果您想将JSON映射到Java类,但您不知道编译类型中的所有属性,则可以利用

If you want to map your JSON to java classes but you don't know all the attributes in compile type, you can leverage the @JsonAnyGetter/@JsonAnySetter annotations. Here is an example based on your JSON that stores the unknown attributes for the Address class in the internal map.

public class JacksonMapping {
    public static final String JSON = "...";

    public static class Attributes {
        public String type;
        public URI url;
    }

    public static class Address {
        public String city;
        public String country;
        public String state;
        public Integer stateCode;
        public String street;
        private final Map<String, Object> otherAttributes = new HashMap<>();

        @JsonAnySetter
        public void setProperty(String name, Object value) {
            otherAttributes.put(name, value);
        }

        @JsonAnyGetter
        public Map<String, Object> getOtherAttributes() {
            return otherAttributes;
        }
    }

    public static class Record {
        @JsonProperty("Id")
        public String id;
        @JsonProperty("Name")
        public String name;
        public Attributes attributes;
        @JsonProperty("Address")
        public Address address;
        @JsonProperty("Phone")
        public String phone;
    }

    public static class RecordList {
        public List<Record> records;
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        RecordList recordList = mapper.readValue(JSON, RecordList.class);
        System.out.println(mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(recordList));
    }

}

在工具的帮助下,我也可以尝试从JSON生成Java对象.例如以下示例: http://www.jsonschema2pojo.org

I can also try to generate java objects from your JSON with a help from a tool. For example this one: http://www.jsonschema2pojo.org

这篇关于如何使用Jackson将JSON数组中的子文档表示为Java Collection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆