首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jackson从POJO形成嵌套JSON

使用Jackson从POJO形成嵌套JSON
EN

Stack Overflow用户
提问于 2020-07-06 17:38:33
回答 1查看 35关注 0票数 0

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

代码语言:javascript
复制
{
   "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

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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;
}

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

代码语言:javascript
复制
  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输出:

代码语言:javascript
复制
{
   "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": []
      }
  ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-06 17:57:16

我能看到的唯一不同之处在于customerEntitlements。我相信你的二传手应该是这样的,

代码语言:javascript
复制
public void customerEntitlements(List<CustomerEntitlements> customerEntitlements)

当你这么做的时候

代码语言:javascript
复制
c.setCustomerEntitlements(Collections.singletonList(ce1)); 
c.setCustomerEntitlements(Collections.singletonList(ce2));

您可以看到ce2覆盖了ce1,相反,您必须这样做,

代码语言:javascript
复制
c.setCustomerEntitlements(Arrays.asList(ce1, ce2));

希望能有所帮助..。

注意:如果排序也是一个问题,我相信您应该知道您的Pojo->Json排序

也许你可以尝试重新排序,

代码语言:javascript
复制
    @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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62753266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档