创建 POJO 以匹配 JSON 结构

Creating POJOs to match a JSON structure

我设计了一个 JSON 结构来表示具有 header 列和 table 行的 table,如下所示。

{
  "header": [
    {
      "fieldType": "STRING",
      "readOnly": true,
      "headerValue": "name"
    },
    {
      "fieldType": "STRING",
      "readOnly": true,
      "headerValue": "description"
    }
  ],
  "rows": [
    [
      {
        "fieldValue" : "engine"            
      },
      {
        "fieldValue" : "this is an engine"
      }
    ],
    [
      {
        "fieldValue" : "engine"            
      },
      {
        "fieldValue" : "this is an engine"
      }
    ],
    [
      {
        "fieldValue" : "engine"            
      },
      {
        "fieldValue" : "this is an engine"
      }
    ],
    [
      {
        "fieldValue" : "engine"            
      },
      {
        "fieldValue" : "this is an engine"
      }
    ]
  ]
}

例如一行

[
  {
    "fieldValue" : "engine"            
  },
  {
    "fieldValue" : "this is an engine"
  }
]

一行中的条目数与 header 列数相匹配。所以 "engine""name" 列,"this is an engine""description"

当我使用 GSON 将我的 POJO 转换为 JSON 字符串时,我必须匹配的最接近此结构的是:

{
  "header": [
    {
      "fieldType": "STRING",
      "readOnly": true,
      "headerValue": "name"
    },
    {
      "fieldType": "STRING",
      "readOnly": true,
      "headerValue": "description"
    }
  ],
  "rows": [
    {
      "fieldValues": [
        "engine",
        "this is an engine"
      ]
    },
    {
      "fieldValues": [
        "engine",
        "this is an engine"
      ]
    },
    {
      "fieldValues": [
        "engine",
        "this is an engine"
      ]
    },
    {
      "fieldValues": [
        "engine",
        "this is an engine"
      ]
    }
  ]
}

这是我用来测试的代码

enum FieldType {

    STRING,
    BOOLEAN,
    NUMBER,
    PHOTO,
    PHOTOLIST;

}

class SurveyFields {

    private List<SurveyColumn> header;  
    private List<SurveyRow> rows;

    public List<SurveyColumn> getHeader() {
        return header;
    }

    public List<SurveyRow> getRows() {
        return rows;
    }

    public void setHeader(List<SurveyColumn> header) {
        this.header = header;
    }

    public void setRows(List<SurveyRow> rows) {
        this.rows = rows;
    }

}

class SurveyColumn {

    private FieldType fieldType;
    private boolean readOnly;
    private String headerValue;

    public static class Builder {
        private FieldType fieldType;
        private boolean readOnly;
        private String headerValue;

        public Builder withFieldType(FieldType fieldType) {
            this.fieldType = fieldType;
            return this;
        }

        public Builder withReadOnly(boolean readOnly) {
            this.readOnly = readOnly;
            return this;
        }

        public Builder withHeaderValue(String headerValue) {
            this.headerValue = headerValue;
            return this;
        }

        public SurveyColumn build() {
            return new SurveyColumn(fieldType, readOnly, headerValue);
        }
    }

    public SurveyColumn(FieldType fieldType, boolean readOnly, String headerValue) {
        this.fieldType = fieldType;
        this.readOnly = readOnly;
        this.headerValue = headerValue;
    }
}

class SurveyRow {

    public static class Builder {
        private String[] fieldValues;

        public Builder withFieldValues(String[] fieldValues) {
            this.fieldValues = fieldValues;
            return this;
        }

        public SurveyRow build() {
            return new SurveyRow(fieldValues);
        }
    }

    private String[] fieldValues;

    public SurveyRow(String[] fieldValues) {
        this.fieldValues = fieldValues;
    }
}

public class TestGson {

    public static void main(String[] args) {

        SurveyFields fields = new SurveyFields();

        fields.setHeader(Arrays.asList(new SurveyColumn[] {
                new SurveyColumn.Builder().withHeaderValue("name").withFieldType(FieldType.STRING).withReadOnly(true)
                        .build(),
                new SurveyColumn.Builder().withHeaderValue("description").withFieldType(FieldType.STRING)
                        .withReadOnly(true).build() }));

        fields.setRows(Arrays.asList(new SurveyRow[] {
                new SurveyRow.Builder().withFieldValues(new String[] { "engine", "this is an engine" }).build(),
                new SurveyRow.Builder().withFieldValues(new String[] { "engine", "this is an engine" }).build(),
                new SurveyRow.Builder().withFieldValues(new String[] { "engine", "this is an engine" }).build(),
                new SurveyRow.Builder().withFieldValues(new String[] { "engine", "this is an engine" }).build()
        }));

        Gson gson = new Gson();
        System.out.println(gson.toJson(fields));

    }

}

如何构造我的 POJO 以匹配预期的 JSON 输出?

如果您从第三方获得 JSON,this site 可能会帮助您从 JSON 生成 POJO。