简体   繁体   English

如何从具有不同字段结构的JSON创建Java obj

[英]How to create Java obj from JSON with varying field structure

I have a json of the below format 我有以下格式的json

[
    {
        "id": "one",
        "type": "Integer",
        "value": "10"
    },
    {
        "id": "two",
        "type": "String",
        "value": "StringValue"
    },
    {
        "id": "three",
        "type": "com.something.special",
        "value": {
            "splFiel1": "filedOne",
            "splFiel2": "fielTwo",
            "splFiel3": "fieldThree"
        }
    }
]

Each array element always will have three fields id,type and value. 每个数组元素始终将具有三个字段id,type和value。 The structure of the field "value" will depend on the field "type" and can change based on that. 字段“值”的结构将取决于字段“类型”,并可以基于此而改变。

I would like to convert this json into Java object, so that i can easily access "value" obj and its sub fields easily . 我想将此json转换为Java对象,以便可以轻松访问“ value” obj及其子字段。 The reason i don't consider this as normal json to java object conversion is due to the varying field structure of "value" field based on the field "type" in the same json. 我不认为这是将json转换为java对象的常规方法,是由于基于同一json中字段“ type”的“ value”字段的字段结构有所不同。

Can this be done?. 能做到吗? I am trying to do this with jackson json, but please do suggest if you have better options. 我正在尝试使用jackson json进行此操作,但是如果您有更好的选择,请提出建议。

Please provide any ideas, suggestions, reference links. 请提供任何想法,建议,参考链接。

You can use following POJO for conversion of your given JSON 您可以使用以下POJO转换给定的JSON

public class Example {

@SerializedName("id")
private String id;
@SerializedName("type")
private String type;
@SerializedName("value")
private String value;
}

For the third field, you can keep it simple string. 对于第三个字段,可以将其保留为简单字符串。 And then whenever you wish to parse its contents to proper construct java class, you can check the type in it and parse the json string into some java object 然后,只要您希望将其内容解析为适当的构造java类,就可以检查其中的类型并将json字符串解析为某个java对象

The JSON file is read using the Google GSON library. 使用Google GSON库读取JSON文件。

Make the DataStructure store the JSON data. 使DataStructure存储JSON数据。 The value field of dataStructure is string type. dataStructure的value字段是字符串类型。 If it stores the JSON string, then again do the JSON parse. 如果它存储JSON字符串,则再次执行JSON解析。

class Data{

    String id;
    String type;
    String value;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    @Override
    public String toString() {
    return "Data [id=" + id + ", type=" + type + ", value=" + value + "]";
    }

}

public class JSONData {

    public static void main(String[] args) throws FileNotFoundException{

         Gson gson = new Gson();
         JsonParser parser = new JsonParser();

        JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream("in.json")));

        JsonArray jArray = parser.parse(reader).getAsJsonArray();

        List<Data> datas = new ArrayList<>();

        for (JsonElement object : jArray) {

            Data data = new Data();

            JsonObject jObject = gson.fromJson(object, JsonObject.class);

            data.setId(jObject.get("id").toString());
            data.setType(jObject.get("type").toString());
            data.setValue(jObject.get("value").toString());

            System.out.println(data);
            datas.add(data);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM