如何在Java中为分层JSON建模

时间:2019-06-21 23:58:07

标签: java json

我是前端开发人员,是后端开发的新手。我的任务是在Java对象中对json建模。现在,我的控制器返回的只是一些模拟数据。

{
   "data":{
      "objectId":25,
      "columnName":[
         "myCategory",
         "myCategoryId"
      ],
      "columnValues":[
         [
            "Category One",
            1
         ],
         [
            "Category Two",
            2
         ],
         [
            "Category Three",
            3
         ],
         [
            "Category Four",
            4
         ],
         [
            "Category Five",
            5
         ]
      ]
   }
}

这是我的尝试。控制器正确返回此json。但这不是很简单吗?我认为应该做的是将 columnName columnValues 数组外推到单独的类中,但是我不确定如何。

package com.category;

import java.util.List;

public class MyObjectData {
    private int objectId;
    private List columnName;
    private List columnValues;

    public int getObjectId() {
        return objectId;
    }
    public void setObjectId(int objectId) {
        this.objectId = objectId;
    }

    public List getColumnName() {
        return columnName;
    }

    public void setColumnName(List colName) {
        this.columnName = colName;
    }

    public List getColumnValues() {
        return columnValues;
    }

    public void setValues(List values) {
        this.columnValues = values;
    }

}

关于 columnNames columnValues ,我觉得我应该在模型中做这样的事情:

private List<ColumnNames> columnNames;
    private List<ColumnValues> columnValues;

    public List<ColumnNames> getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(List<ColumnNames> columnNames) {
        this.columnNames = columnNames;
    }

    public List<ColumnValues> getColumnValues() {
        return columnValues;
    }

    public void setColumnValues(List<ColumnValues> columnValues) {
        this.columnValues = columnValues;
    }

然后我将为他们提供两个单独的类,如下所示:

package com.category;

import java.util.List;

public class ColumnName {

    private String columnName;

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

}

package com.category;

import java.util.List;

public class ColumnValue {

    private String columnValue;
    private int columnValueId;

    public String getColumnValue() {
        return columnValue;
    }

    public void setColumnValue(String columnValue) {
        this.columnValue = columnValue;
    }

    public String getColumnValueId() {
        return columnValueId;
    }

    public void setColumnValueId(int columnValueId) {
        this.columnValueId = columnValueId;
    }

}

我觉得我拥有所有合适的作品,但是只是不确定是否这是比我最初的尝试更好的方法……可行。只是在寻找输入。提前致谢。

1 个答案:

答案 0 :(得分:1)

在您的结构中,columnValues实际上是表中具有两列的行:myCategorymyCategoryId

更多的“面向对象” Java类可能是这样的:

public class MyObjectData {

    private int objectId;

    private List<MyObjectRow> columnValues; // I would have named this as rows

}

public class MyObjectRow {

    private String myCategory;

    private String myCategoryId;

}

现在,您需要一个自定义序列化程序以将其转换为所需的JSON结构:

public class MyObjectDataSerializer extends StdSerializer<MyObjectData> {

    public MyObjectDataSerializer() {
        super(MyObjectData.class);
    }

    public void serialize(MyObjectData value, JsonGenerator generator, SerializerProvider provider) throws IOException {
        generator.writeStartObject();
        generator.writeNumberField("objectId", value.getObjectId());

        generator.writeArrayFieldStart("columnName");
        generator.writeString("myCategory");
        generator.writeString("myCategoryId");
        generator.writeEndArray();

        generator.writeArrayFieldStart("columnValues");
        for (MyObjectRow row : value.getColumnValues()) {
            generator.writeStartArray();
            generator.writeString(row.getMyCategory());
            generator.writeNumber(row.getMyCategoryId());
            generator.writeEndArray();
        }
        generator.writeEndArray();


        generator.writeEndObject();
    }
}

注意:您可以使用反射来动态提取字段名称和值。

然后,您可以将MyObjectData对象序列化为期望的形式:

public class MyObjectDataSerializerTest {

    @Test
    public void shouldCustomSerializeMyObjectData() throws Exception {
        ObjectMapper mapper = new ObjectMapper();

        SimpleModule module = new SimpleModule();
        module.addSerializer(MyObjectData.class, new MyObjectDataSerializer());
        mapper.registerModule(module);

        MyObjectData myObjectData = new MyObjectData();
        myObjectData.setObjectId(25);
        myObjectData.setColumnValues(Arrays.asList(
                new MyObjectRow("Category One", 1),
                new MyObjectRow("Category Two", 2),
                new MyObjectRow("Category Three", 3)
        ));


        String serialized = mapper.writeValueAsString(myObjectData);

        assertThat(serialized, equalTo("{\"objectId\":25,\"columnName\":[\"myCategory\",\"myCategoryId\"],\"columnValues\":[[\"Category One\",1],[\"Category Two\",2],[\"Category Three\",3]]}\n"));
    }

}