Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建POJO以匹配JSON结构_Java_Json_Gson_Pojo - Fatal编程技术网

Java 创建POJO以匹配JSON结构

Java 创建POJO以匹配JSON结构,java,json,gson,pojo,Java,Json,Gson,Pojo,我设计了一个JSON结构来表示一个带有标题列和表行的表,如下所示 { "header": [ { "fieldType": "STRING", "readOnly": true, "headerValue": "name" }, { "fieldType": "STRING", "readOnly": true, "headerValue": "description" } ],

我设计了一个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"
  }
]
行中的条目数与标题列数匹配。因此,
“引擎”
“名称”
列,
“这是一个引擎”
“说明”

当我使用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));

    }

}
enum字段类型{
一串
布尔,
数字,
照片,
照片列表;
}
班级调查场{
私有列表头;
私有列表行;
公共列表getHeader(){
返回头;
}
公共列表getRows(){
返回行;
}
公共无效集合标题(列表标题){
this.header=头;
}
公共无效集合行(列表行){
this.rows=行;
}
}
类调查列{
专用字段类型字段类型;
私有布尔只读;
私有字符串头值;
公共静态类生成器{
专用字段类型字段类型;
私有布尔只读;
私有字符串头值;
具有字段类型的公共生成器(字段类型字段类型){
this.fieldType=fieldType;
归还这个;
}
带只读的公共生成器(布尔只读){
this.readOnly=readOnly;
归还这个;
}
具有headerValue的公共生成器(字符串headerValue){
this.headerValue=headerValue;
归还这个;
}
公共调查栏目建设(){
返回新的SurveyColumn(fieldType、readOnly、headerValue);
}
}
public SurveyColumn(FieldType FieldType、boolean readOnly、String headerValue){
this.fieldType=fieldType;
this.readOnly=readOnly;
this.headerValue=headerValue;
}
}
阶级调查{
公共静态类生成器{
私有字符串[]字段值;
带字段值的公共生成器(字符串[]字段值){
this.fieldValues=字段值;
归还这个;
}
公共勘测建筑(){
返回新的SurveyRow(字段值);
}
}
私有字符串[]字段值;
公共调查(字符串[]字段值){
this.fieldValues=字段值;
}
}
公共类TestGson{
公共静态void main(字符串[]args){
SurveyFields=新的SurveyFields();
fields.setHeader(Arrays.asList)(新的SurveyColumn[]{
新建SurveyColumn.Builder().withHeaderValue(“名称”)。withFieldType(FieldType.STRING)。withReadOnly(true)
.build(),
新建SurveyColumn.Builder().withHeaderValue(“description”).withFieldType(FieldType.STRING)
.withReadOnly(true).build()});
fields.setRows(Arrays.asList)(新SurveyRow[]{
新建SurveyRow.Builder().WithFieldValue(新字符串[]{“引擎”,“这是一个引擎”})。build(),
新建SurveyRow.Builder().WithFieldValue(新字符串[]{“引擎”,“这是一个引擎”})。build(),
新建SurveyRow.Builder().WithFieldValue(新字符串[]{“引擎”,“这是一个引擎”})。build(),
新建SurveyRow.Builder().WithFieldValue(新字符串[]{“引擎”,“这是一个引擎”}).build()
}));
Gson Gson=新的Gson();
System.out.println(gson.toJson(字段));
}
}

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

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

到目前为止做得很好。。但是确切的问题是什么?我已经澄清了这一点,现在有了一些新的补充,这是一个有用的网站!单击“预览我的JSON示例”只会显示一个空白页面?@user2987444您是否选择源类型为“JSON”而不是“JSON模式”?它的结构很好,但不准确。