首页 > 解决方案 > 使用 Jackson 从 POJO 形成嵌套 JSON

问题描述

我正在努力从我的 POJO 中形成正确的 JSON 对象。抱歉,这有点啰嗦,我只是在学习 Java -> JSON 工具。我需要以下结构:

{
   "firstName":"John",
   "lastName":"Doe",
   "addressLine1":"1 High Street",
   "addressLine2":"",
   "addressLine3":"",
   "addressLine4":"Buffalo",
   "zipCode":"22313",
   "customerEntitlements":[
      {
            "entitlementCode": "6",
            "entitlementType": "G",
            "validFrom": "1980-04-22",
            "validTo": "2026-11-17",
            "customerRestrictions": []
          },
          {
            "entitlementCode": "4",
            "entitlementType": "L",
            "validFrom": "1980-04-22",
            "validTo": "2026-11-17",
            "customerRestrictions": []
          }
   ],
   "customerPresent":null,
   "customerConsentFormSighted":null,
   "customerCheckConsent":true,
   "dob":"1982-01-01",
   "customerEndorsements":[]
}

我现在的 POJO:

Customers

@Data
public class Customers {


    @JsonProperty("firstName")
    private String firstName;
    @JsonProperty("lastName")
    private String lastName;
    @JsonProperty("addressLine1")
    private String addressLine1;
    @JsonProperty("addressLine2")
    private String addressLine2;
    @JsonProperty("addressLine3")
    private String addressLine3;
    @JsonProperty("addressLine4")
    private String addressLine4;
    @JsonProperty("zipcode")
    private String zipcode;
    @JsonProperty("customerPresent")
    private String customerPresent;
    @JsonProperty("customerConsentFormSighted")
    private String customerConsentFormSighted;
    @JsonProperty("customerCheckConsent")
    private Boolean customerCheckConsent;
    @JsonProperty("dob")
    private String dob;
    @JsonProperty("customerEndorsements")
    private List<Object> customerEndorsements = null;
    @JsonProperty("customerEntitlements")
    private List<CustomerEntitlements> customerEntitlements;

}

CustomerEntitlements

@Data
public class CustomerEntitlements {

    @JsonProperty("entitlementCode")
    private String entitlementCode;
    @JsonProperty("entitlementType")
    private String entitlementType;
    @JsonProperty("validFrom")
    private String validFrom;
    @JsonProperty("validTo")
    private String validTo;
    @JsonProperty("customerRestrictions")
    private List<Object> customerRestrictions = null;
}

我添加准备发送的有效负载的代码:

  c = new Customers();

        ce1 = new CustomerEntitlements();
        ce2 = new CustomerEntitlements();

        ce1.setEntitlementCode("B");
        ce1.setEntitlementType("F");
        ce1.setValidFrom("1980-04-22");
        ce1.setValidTo("2026-11-17");
        ce1.setCustomerrRestrictions(new JSONArray());

        ce2.setEntitlementCode("D");
        ce2.setEntitlementType("F");
        ce2.setValidFrom("1980-04-22");
        ce2.setValidTo("2026-11-17");
        ce2.setCustomerrRestrictions(new JSONArray());

   
        c.setFirstName("John);
        c.setLastName("Doe");
        c.setAddressLine1("1 High Street);
        c.setAddressLine2("");
        c.setAddressLine3("");
        c.setAddressLine4("Buffalo");
        c.setZipcode("22313");
        c.setCustomerEntitlements(Arrays.asList(ce1, ce2);
        c.setCustomerPresent(null);
        c.setCustomerConsentFormSighted(null);
        c.setCustomerCheckConsent(true);
        c.setDob("1982-01-01");
        c.setCustomerEndorsements(new JSONArray());

我的(格式错误的)来自代码的 JSON 输出:

{
   "firstName":"John",
   "lastName":"Doe",
   "addressLine1":"1 High Street",
   "addressLine2":"",
   "addressLine3":"",
   "addressLine4":"Buffalo",
   "zipcode":"22313",
   "customerPresent":null,
   "customerConsentFormSighted":null,
   "dob":"1956-01-01",
   "customerEndorsements":[

   ],
   "customerEntitlements":[
  {
        "entitlementCode": "6",
        "entitlementType": "G",
        "validFrom": "1980-04-22",
        "validTo": "2026-11-17",
        "customerRestrictions": []
      },
      {
        "entitlementCode": "4",
        "entitlementType": "L",
        "validFrom": "1980-04-22",
        "validTo": "2026-11-17",
        "customerRestrictions": []
      }
  ]
}

标签: javajsonjackson

解决方案


我可以看到唯一的区别是与customerEntitlements. 我相信你的二传手会是这样的,

public void customerEntitlements(List<CustomerEntitlements> customerEntitlements)

当你这样做时,

c.setCustomerEntitlements(Collections.singletonList(ce1)); 
c.setCustomerEntitlements(Collections.singletonList(ce2));

在那里你可以看到 ce2 覆盖了 ce1,而不是你必须这样做,

c.setCustomerEntitlements(Arrays.asList(ce1, ce2));

希望能帮助到你..

注意:如果订购也是一个问题,我相信您会知道您的Pojo->Json订购

也许你可以尝试重新订购,

    @JsonProperty("firstName")
    private String firstName;
    @JsonProperty("lastName")
    private String lastName;
    @JsonProperty("addressLine1")
    private String addressLine1;
    @JsonProperty("addressLine2")
    private String addressLine2;
    @JsonProperty("addressLine3")
    private String addressLine3;
    @JsonProperty("addressLine4")
    private String addressLine4;
    @JsonProperty("zipcode")
    private String zipcode;
    @JsonProperty("customerEntitlements")
    private List<CustomerEntitlements> customerEntitlements;
    @JsonProperty("customerPresent")
    private String customerPresent;
    @JsonProperty("customerConsentFormSighted")
    private String customerConsentFormSighted;
    @JsonProperty("customerCheckConsent")
    private Boolean customerCheckConsent;
    @JsonProperty("dob")
    private String dob;
    @JsonProperty("customerEndorsements")
    private List<Object> customerEndorsements = null;

推荐阅读