如何将此Java对象转换为JSON?

时间:2016-12-14 17:33:13

标签: java json eclipse

我希望我的JSON看起来像这样:

"optInInfoList": {

  "optInInfo": [
    {
      "optInCode": "123445 (max 20)",

      "optInName": "WORDSGOHERE (max 255)",

      "optInDescription": "DESCRIPTION HERE (max 4000)"

    }

  ]
 "optInInfo": [
    {
      "optInCode": "123445 (max 20)",

      "optInName": "WORDSGOHERE (max 255)",

      "optInDescription": "DESCRIPTION HERE (max 4000)"

    }

  ]
}
到目前为止

代码:

package com.sprint.eai.createcreditaccountagreement.bo;

public class OptInInfo {

    private String optInCode;
    private String optInName;
    private String optInDescription;

    public OptInInfo() {
    }

    public OptInInfo(String optInCode, String optInName, String optInDescription) {
        this.optInCode = optInCode;
        this.optInName = optInName;
        this.optInDescription = optInDescription;

}

public String getOptInCode() {
    return optInCode;
}

public void setOptInCode(String optInCode) {
    this.optInCode = optInCode;
}

public String getOptInName() {
    return optInName;
}

public void setOptInName(String optInName) {
    this.optInName = optInName;
}

public String getOptInDescription() {
    return optInDescription;
}

public void setOptInDescription(String optInDescription) {
    this.optInDescription = optInDescription;
}

}

并且

package com.sprint.eai.createcreditaccountagreement.bo;

import java.util.ArrayList;

public class CreditAccountAgreement {

    private ArrayList<OptInInfo> optInInfoList;

    public CreditAccountAgreement() {
    }

    public ArrayList<OptInInfo> getOptInInfoList() {
        return optInInfoList;
    }

    public void setOptInInfoList(ArrayList<OptInInfo> optInInfoList) {
        this.optInInfoList = optInInfoList;
    }

}

我只是错过了如何用Jackson将Java对象转换为JSON的部分:

private static ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(Include.NON_NULL);

String jsonRequest = mapper.writeValueAsString(cstRequest);

0 个答案:

没有答案