java 使用java访问嵌套的JSON对象值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17062945/
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-11-01 00:51:57  来源:igfitidea点击:

Access nested JSON object value using java

javajson

提问by user2436651

{
    "files": {
        "f1.png": {
            "intext": "A",
            "inval": 0,
            "inbinary": false
        },
        "f2.png": {
            "intext": "A",
            "inval": 0,
            "inbinary": true
        }
    }
}

How to access value of inval when the f1.png value is not fixed. i.e. the name of file can be anything, its not known so how can I access value for inval field for various files in this JSON using Java?

当 f1.png 值不固定时如何访问 inval 的值。即文件的名称可以是任何东西,它是未知的,那么如何使用 Java 访问此 JSON 中各种文件的 inval 字段的值?

回答by Pritesh Shah

Please try below code,

请尝试以下代码,

import org.json.JSONException;
import org.json.JSONObject;
public static void main(String[] args) {
        String jsonString = "{\"files\": {\"f1.png\": {\"intext\": \"A\",\"inval\": 0,\"inbinary\": false}, \"f2.png\": {\"intext\": \"A\",\"inval\": 0,\"inbinary\": true}}}";
        try {
            JSONObject jsonObject =new JSONObject(jsonString);
            JSONObject jsonChildObject = (JSONObject)jsonObject.get("files");
            Iterator iterator  = jsonChildObject.keys();
            String key = null;
            while(iterator.hasNext()){
                key = (String)iterator.next();
                System.out.println("inval value: "+((JSONObject)jsonChildObject.get(key)).get("inval"));
            }
        }
        catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Hope it solves your issue

希望它能解决你的问题

回答by fge

Using Hymansonand JsonNode, you'd do:

使用HymansonJsonNode,你会这样做:

private static final ObjectReader READER = new ObjectMapper()
    .getReader;

// blah

// read the node
final JsonNode node = READER.readTree(fromWhatever);

// access the inner "files" member
final JsonNode filesNode = node.get("files");

to access the inner object.

访问内部对象。

Then to walk the filesNodeobject you'd do:

然后走filesNode你要做的对象:

final Iterator<Map.Entry<String, JsonNode>> iterator = filesNode.fields();
Map.Entry<String, JsonNode> entry;
while (iterator.hasNext()) {
    entry = iterator.next();
    // the "inval" field is entry.getValue().get("inval")
}

If you can use this projectthis becomes more simple:

如果您可以使用此项目,这将变得更加简单:

// or .fromFile(), .fromReader(), others
final JsonNode node = JsonLoader.fromString(whatever);

final Map<String, JsonNode> map = HymansonUtils.nodeToMap(node.get("files"));
// walk the map

回答by best wishes

You can use JsonPath library to access child elements. https://github.com/json-path/JsonPath

您可以使用 JsonPath 库来访问子元素。 https://github.com/json-path/JsonPath

It can be as simple as

它可以很简单

List<String> names = JsonPath.read(json, "$.files.*);

With some modifications.

经过一些修改。

回答by Jabongg

for nested array type,

对于嵌套数组类型,

{
"combo":    [
                {"field":"divisions"},
                {"field":"lob"}
]

} below code will help.

下面的代码会有所帮助。

Iterator<?> keys = jsnObj.keys();

    while(keys.hasNext()) {
        String key = (String) keys.next();
        JSONArray list = (JSONArray) jsnObj.get(key);
        if(list==null || list.length()==0)
            return null;
        List<String> comboList = new ArrayList<String>();
        for(int i=0;i<list.length();i++){
            JSONObject jsonObject =  (JSONObject)list.get(i);
            if(jsonObject==null)
                continue;
            comboList.add((String)jsonObject.get("fields"));
        }
    }