我设计了一个JSON结构来表示一个包含标题列和表行的表,如下所示。
{
"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"
}
]
一行中的条目数与标题列的数量相匹配。因此"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输出?