java 如何将具有动态字段的 Json 字符串转换为对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15389110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:25:10  来源:igfitidea点击:

How to convert Json String with dynamic fields to Object?

javajson

提问by Maxim Shoustin

I have the followed snipets of Json String:

我有以下 Json 字符串的片段:

 {
  "networks": {
    "tech11": {
        "id": "1",
        "name": "IDEN"
    },
    "tech12": {
        "id": "2",
        "name": "EVDO_B"
    }
 }
}

I use some methods to convert this String to Object:

我使用一些方法将此字符串转换为对象:

    private static Gson mGson = new Gson();

    ...

public static WebObjectResponse convertJsonToObject(String jsonString) {

    WebObjectResponse webObjectResponse = null;


    if(jsonString != null && jsonString.length() > 1){
        webObjectResponse = mGson.fromJson(jsonString, WebObjectResponse.class);
    }

    return webObjectResponse;

}

Where WebObjectResponseis class that should represent above mentioned String.

WebObjectResponse应该代表上述字符串的类在哪里。

Its not complicated if I get static fields. But in my case the values have different names: tech11, tech12....

如果我得到静态字段,它并不复杂。但在我的情况下,这些值有不同的名称:tech11, tech12....

I can use @SerializedNamebut its works in specific cases like convert "class" to "class_". As you see networksObject defined as list of techObjects but with different post-fix.

我可以使用@SerializedName但它在特定情况下有效,例如将“class”转换为“class_”。如您所见,networks对象定义为tech对象列表,但具有不同的后修复。

public class WebObjectResponse{
 private DataInfoList networks = null;
} 

This is static implementation, i defined 2 values tech11and tech12but next response might be techXX

这是静态实现,我定义了 2 个值tech11tech12但下一个响应可能是techXX

public class DataInfoList {
 private DataInfo tech11 = null;
 private DataInfo tech12 = null;
}


public class DataInfo {
 private String id = null;
 private String name = null;
}

What is the good way to convert current Json String to Object where list of elements are Objects too and have different names?

将当前 Json 字符串转换为对象的好方法是什么,其中元素列表也是对象并且具有不同的名称?

Thank you.

谢谢你。

采纳答案by Sotirios Delimanolis

Use a Map!

使用地图!

I would do the following

我会做以下

public class WebObjectResponse {
     private Map<String, DataInfo> networks;
} 

public class DataInfo {
     private String id = null;
     private String name = null;
}

// later
Gson gson = new Gson();
String json = "{\"networks\": {\"tech11\": { \"id\": \"1\",\"name\": \"IDEN\" },  \"tech12\": { \"id\": \"2\", \"name\": \"EVDO_B\" }    }}";

WebObjectResponse response = gson.fromJson(json, WebObjectResponse .class);

For each object in json networks, a new entry will be added to the Mapfield of your class WebObjectResponse. You then reference them by techXXor iterate through the keyset.

对于 json 中的每个对象,networks都会在Map您的类的字段中添加一个新条目WebObjectResponse。然后您techXX通过键集引用它们或迭代它们。

Assuming a structure like this

假设这样的结构

{
  "networks": {
    "tech11": {
        "id": "1",
        "name": "IDEN"
    },
    "tech12": {
        "id": "2",
        "name": "EVDO_B"
    },
    "tech13": {
        "id": "3",
        "name": "WOHOO"
    }, ...
  }
}

回答by karmanaut

We would need your class structure for more details.

我们需要您的类结构以获取更多详细信息。

As far as I am aware, I think you will need to have some mappings defined somewhere (I used xml's) and then try to match json with one of the mappings to create objects.

据我所知,我认为您需要在某处定义一些映射(我使用了 xml),然后尝试将 json 与其中一个映射匹配以创建对象。

Google gson is good. I did it in Hymanson

谷歌gson很好。我在Hyman逊做过

Also, converting objects should be trivial. But since you might have variable fields like tech11and tech12, you might want to store the "network" value as a string and then extract fields out of it when required.

此外,转换对象应该是微不足道的。但是由于您可能有像tech11and这样的变量字段tech12,您可能希望将“网络”值存储为字符串,然后在需要时从中提取字段。

Hope I could help.

希望我能帮上忙。

Edit : Sotirious nails it.

编辑:Sotirious 指出它。