【问题标题】:Java mapping JSON with POJO using jacksonJava 使用 jackson 将 JSON 与 POJO 映射
【发布时间】:2021-01-10 13:13:42
【问题描述】:

我想使用带有 jakson 注释的 POJOS 创建这个 JSON。当我创建一个没有 @JsonProperty 注释的新类来表示最后一个 {"id":"123ccc","role":"dddd"} 时遇到的问题,它默认采用类名并创建类似“customer”的东西:{"id":"123ccc","role":"dddd"}.

我要构建的 JSON 结构

{
  "relatedParty": [
    {
      "contact": [
        {
          "mediumType": "xxx",
          "characteristic": {
            "city": "xxx",
            "country": "xxx"
          }
        },
        {
          "mediumType": "yyy",
          "characteristic": {
            "emailAddress": "yyy@yy.yyy"
          }
        }
      ],
      "role": "ccc",
      "fullName": "ccc"
    },
    {
      "id": "123ccc",
      "role": "dddd"
    }
  ]
}

我从以下代码收到的 JSON。

 {
  "relatedParty": [
    {
      "contact": [
        {
          "mediumType": "xxx",
          "characteristic": {
            "city": "xxx",
            "country": "xxx"
          }
        },
        {
          "mediumType": "yyy",
          "characteristic": {
            "emailAddress": "yyy@yy.yyy"
          }
        }
      ],
      "role": "ccc",
      "fullName": "ccc"
    },
    "customer" : {
      "id": "123ccc",
      "role": "dddd"
    }
  ]
}

获取确切的 JSON 格式作为图像的解决方法是什么。当前的实现如下。

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class RelatedParty {

    @JsonProperty(value = "contact")
    private List<Contact> contact;

    @JsonProperty(value = "role")
    private String role;

    @JsonProperty(value = "fullName")
    private String fullName;

    private Customer customer;

    public List<Contact> getContact() {
        return contact;
    }

    public void setContact(List<Contact> contact) {
        this.contact = contact;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
}

    public class Customer {

    @JsonProperty(value = "id")
    private String id;

    @JsonProperty(value = "role")
    private String role;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

【问题讨论】:

    标签: java json annotations jaxb


    【解决方案1】:

    您需要创建其他不同的 POJO 类来正确建模 JSON。基本上,JSON 数组将在 Java 列表中处理,而 JSON 对象将在 Java 类中处理。

    从 JSON 的内部(最嵌套的级别)开始,逐步解决:

    注意:这里没有显示 getter 和 setter

    Characteristic.java

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Characteristic {
    
        @JsonProperty("city")
        private String city;
        @JsonProperty("country")
        private String country;
        @JsonProperty("emailAddress")
        private String emailAddress;
    }
    

    Contact.java(包含我们的特征):

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Contact {
    
        @JsonProperty("mediumType")
        private String mediumType;
        @JsonProperty("characteristic")
        private Characteristic characteristic;
    }
    

    以上两个类处理最里面的对象。如果我们从您的目标 JSON 中删除它们,则会留下以下内容:

    {
        "relatedParty": [{
            "contact": [...],
            "role": "ccc",
            "fullName": "ccc"
        }, {
            "role": "dddd",
            "id": "123ccc"
        }]
    }
    

    请注意,contact 字段是 JSON 数组,而不是对象 - 因此我们不会创建 Java Contact 类(用于 JSON 对象)。

    为了处理上述问题,我创建了另外两个类:

    RelatedPartyInner.java(包含联系人列表)

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class RelatedParty_ {
    
        @JsonProperty("contact")
        private List<Contact> contact = null;
        @JsonProperty("role")
        private String role;
        @JsonProperty("fullName")
        private String fullName;
        @JsonProperty("id")
        private String id;
    }
    

    RelatedParty.java(将所有内容包装在外部对象中):

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class RelatedParty {
    
        @JsonProperty("relatedParty")
        private List<RelatedPartyInner> relatedParty = null;
    }
    

    为了测试这一点,我创建了以下数据:

    Characteristic chr1 = new Characteristic();
    chr1.setCity("xxx");
    chr1.setCountry("xxx");
    Characteristic chr2 = new Characteristic();
    chr2.setEmailAddress("yyy@yy.yyy");
    
    Contact con1 = new Contact();
    con1.setMediumType("xxx");
    con1.setCharacteristic(chr1);
    Contact con2 = new Contact();
    con2.setMediumType("yyy");
    con2.setCharacteristic(chr2);
    List<Contact> cons = new ArrayList<>();
    cons.add(con1);
    cons.add(con2);
    
    RelatedPartyInner rpi1 = new RelatedPartyInner();
    rpi1.setContact(cons);
    rpi1.setRole("ccc");
    rpi1.setFullName("ccc");
    RelatedPartyInner rpi2 = new RelatedPartyInner();
    rpi2.setId("123ccc");
    rpi2.setRole("dddd");
    List<RelatedPartyInner> rpis = new ArrayList<>();
    rpis.add(rpi1);
    rpis.add(rpi2);
    
    RelatedParty rp = new RelatedParty();
    rp.setRelatedParty(rpis);
    

    最后,我们可以生成 JSON:

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.writeValue(new File("rp.json"), rp);
    

    生成的文件包含以下内容:

    {
        "relatedParty": [{
            "contact": [{
                "mediumType": "xxx",
                "characteristic": {
                    "city": "xxx",
                    "country": "xxx"
                }
            }, {
                "mediumType": "yyy",
                "characteristic": {
                    "emailAddress": "yyy@yy.yyy"
                }
            }],
            "role": "ccc",
            "fullName": "ccc"
        }, {
            "role": "dddd",
            "id": "123ccc"
        }]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多