Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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
如何从具有不同字段结构的JSON创建Java obj_Java_Json_Jackson - Fatal编程技术网

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

如何从具有不同字段结构的JSON创建Java obj,java,json,jackson,Java,Json,Jackson,我有以下格式的json [ { "id": "one", "type": "Integer", "value": "10" }, { "id": "two", "type": "String", "value": "StringValue" }, { "id": "three", "type": "com.something.sp

我有以下格式的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"
        }
    }
]
每个数组元素始终有三个字段id、类型和值。 字段“值”的结构将取决于字段“类型”,并且可以基于该字段进行更改

我想将这个json转换成Java对象,这样我就可以轻松地访问“value”obj及其子字段。我不认为这是JSON到java对象转换的原因,是因为在同一个JSON中,字段“类型”的“值”字段的字段结构不同。 这能做到吗?。 我正试图用jackson json来实现这一点,但如果您有更好的选择,请务必提出建议


请提供任何想法、建议和参考链接。

您可以使用以下POJO转换给定的JSON

public class Example {

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

对于第三个字段,可以将其保留为简单字符串。然后,每当您希望解析其内容以正确构造java类时,可以检查其中的类型并将json字符串解析为某个java对象,您可以使用以下POJO转换给定的json

public class Example {

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

对于第三个字段,可以将其保留为简单字符串。然后,每当您希望解析其内容以正确构造java类时,可以检查其中的类型并将json字符串解析为某个java对象,json文件将使用Google库读取

使数据结构存储JSON数据。数据结构的值字段是字符串类型。如果它存储了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);
        }
    }
}
类数据{
字符串id;
字符串类型;
字符串值;
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共字符串getValue(){
返回值;
}
公共void设置值(字符串值){
这个值=值;
}
@凌驾
公共字符串toString(){
返回“Data[id=“+id+”,type=“+type+”,value=“+value+”]”;
}
}
公共类JSONData{
公共静态void main(字符串[]args)引发FileNotFoundException{
Gson Gson=新的Gson();
JsonParser=新的JsonParser();
JsonReader=newJSONReader(newInputStreamReader(newFileInputStream(“in.json”)));
JsonArray jArray=parser.parse(reader.getAsJsonArray();
列表数据=新的ArrayList();
for(JsonElement对象:jArray){
数据=新数据();
JsonObject jObject=gson.fromJson(对象,JsonObject.class);
data.setId(jObject.get(“id”).toString());
data.setType(jObject.get(“type”).toString());
data.setValue(jObject.get(“value”).toString();
系统输出打印项次(数据);
添加(数据);
}
}
}

使用Google库读取JSON文件

使数据结构存储JSON数据。数据结构的值字段是字符串类型。如果它存储了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);
        }
    }
}
类数据{
字符串id;
字符串类型;
字符串值;
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共字符串getValue(){
返回值;
}
公共void设置值(字符串值){
这个值=值;
}
@凌驾
公共字符串toString(){
返回“Data[id=“+id+”,type=“+type+”,value=“+value+”]”;
}
}
公共类JSONData{
公共静态void main(字符串[]args)引发FileNotFoundException{
Gson Gson=新的Gson();
JsonParser=新的JsonParser();
JsonReader=newJSONReader(newInputStreamReader(newFileInputStream(“in.json”)));
JsonArray jArray=parser.parse(reader.getAsJsonArray();
列表数据=新的ArrayList();
for(JsonElement对象:jArray){
数据=新数据();
JsonObject jObject=gson.fromJson(对象,JsonObject.class);
data.setId(jObject.get(“id”).toString());
data.setType(jObject.get(“type”).toString());
data.setValue(jObject.get(“value”).toString();
系统输出打印项次(数据);
添加(数据);
}
}
}

我建议您首先使用org.json将字符串解析为数组,循环遍历每个对象,使用解析对象的get方法查找类型,然后将对象映射到该类。我建议您首先使用org.json将字符串解析为数组,循环遍历每个对象,使用解析对象的get方法查找类型,然后将对象映射到该类。