Java 使用 jackson 将 JSON 映射到 POJO

Java mapping JSON with POJO using jackson

我想使用 jakson 注释的 POJOS 创建这个 JSON。当我创建一个没有 @JsonProperty 注释的新 class 来表示最后一个 {"id":"123ccc","role":"dddd"} 时遇到的问题,它默认采用 class命名并创建类似 "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;
    }
}

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

中处理

从 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;
}

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

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

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

为了处理上面的问题,我又创建了两个 classes:

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"
    }]
}