将POJO类转换为嵌套的Json

时间:2019-06-30 18:46:20

标签: jackson okhttp pojo

我在使用POJO生成JSON时遇到问题。为此,我使用了jackson-databind 2.9.9。 以下是我的Pojo课程。 ExampleModel包含getter和setter以及一个子类。

public class ExampleModel {

private String summary;
List<Custom_fields> customFieldsList;

public ExampleModel() {
}

public class Custom_fields
{
    private List<Map<String, String>> customField;
    private String value;

    public void setValue(String value){
        this.value = value;
    }
    public String getValue(){
        return this.value;
    }

    @JsonProperty("custom_fields")
    public List<Map<String, String>> getCustomField() {
        return customField;
    }
    public void setCustomField(List<Map<String, String>> customField) {
        this.customField = customField;
    }
}

public String getSummary() {
    return summary;
}
public void setSummary(String summary) {
    this.summary = summary;
}
}

函数类     公共类ExampleFuntion {

public ExampleFunction() {
}

private static ExampleModel exampleModel = new ExampleModel();
private static ExampleModel.Custom_fields custField = exampleModel.new Custom_fields();

public static ExampleModel createData() {

    exampleModel.setSummary(strSummary);
    List<Map<String, String>> customField = new ArrayList<Map<String, String>>();
    Map<String, String> mapCustomValues = new HashMap<String, String>();
    mapCustomValues.put("name", strRftVersion);
    customField.add(mapCustomValues);
    custField.setCustomField(customField);
    custField.setValue("V1.1010");

    exampleModel.customFieldsList.add(custField);  
    // is this right way to do

    return exampleModel;
}
}

以这种JSON格式,我想在Pojo类之上进行转换

{
  "summary" : "Example Summary",
  "custom_fields": [
    {
        "field": {
            "name": "Dummy Name"
        },
        "value": "Seattle"
    }
  ]
}

0 个答案:

没有答案