使用 JSON Java 检索嵌套数组值

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

Retrieving nested arrays values with JSON Java

javaarraysjson

提问by Pablo Fernández

I'm struggling to retrieve some values from a JSON file formatted like this one:

我正在努力从格式如下的 JSON 文件中检索一些值:

{
  "search": {
    "entry": [
      {
        "found": "identity=9454532,l=big,ton=grand,k=molvi",
        "attribute": [
          {
            "name": "firstname",
            "value": [
              "Lucas"
            ]
          },
          {
            "name": "lastname",
            "value": [
              "Brandon"
            ]
          }
        ]
      }
    ],
    "return": {
      "code": 0,
      "message": "Success",
      "count": 1
    }
  }
}

I have tried different approaches (json, gson, jayway-JsonPath) but I don't manage to get the values from the "attribute" array, only those from the first array. I don't know how to specify that "attribute" is an JSONArray and not a JSONObject or how to set the proper path to it. This is the last code I was playing with which stops when it founds an array:

我尝试了不同的方法(json、gson、jayway-JsonPath),但我无法从“属性”数组中获取值,只能从第一个数组中获取值。我不知道如何指定“属性”是 JSONArray 而不是 JSONObject 或如何设置正确的路径。这是我使用的最后一个代码,它在找到数组时停止:

public void String nameObtain (String email) throws IOException{

String link = "http://jsonfile/" + email;

    JSONObject json = readJsonFromUrl(link);        
    JSONObject rootObject = json.getJSONObject("search");
    JSONArray firstArray = rootObject.getJSONArray("entry");

for (int i = 0, size = firstArray.length(); i < size; i++) {
    JSONObject objectInArray = firstArray.getJSONObject(i);

    String[] elementNames = JSONObject.getNames(objectInArray);
    System.out.printf("%d ELEMENTS IN CURRENT OBJECT:\n", elementNames.length);
    for (String elementName : elementNames) {
        String value = objectInArray.getString(elementName);
        System.out.printf("name=%s, value=%s\n", elementName, value);
    }

}

}

What I would like to do is to get the values Lucas or Brandon. Any help will be much appreciated!

我想做的是获得 Lucas 或 Brandon 的值。任何帮助都感激不尽!

回答by Rishal dev singh

Libraries used:

使用的库:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

Check the below code and step wise parsing

检查以下代码并逐步解析

    JSONObject search = (JSONObject) jsonObject.get("search");//1
    JSONArray entry = (JSONArray) search.get("entry");//2
    for (int i = 0; i < entry.size(); i++) {
        JSONObject jsonObject1 = (JSONObject) entry.get(i);//3
        JSONArray jsonarray1 = (JSONArray) jsonObject1.get("attribute");//4
        for (int j = 0; j < jsonarray1.size(); j++) {
            System.out.println(((JSONObject) jsonarray1.get(j)).get(
                    "value").toString());//5

        }

    }

It will give the values mentioned step wise:

它将逐步给出提到的值:

1) {"entry":[{"found":"identity=9454532,l=big,ton=grand,k=molvi","attribute":[{"name":"firstname","value":["Lucas"]},{"name":"lastname","value":["Brandon"]}]}],"return":{"code":0,"count":1,"message":"Success"}}

2) [{"found":"identity=9454532,l=big,ton=grand,k=molvi","attribute":[{"name":"firstname","value":["Lucas"]},{"name":"lastname","value":["Brandon"]}]}]

3) {"found":"identity=9454532,l=big,ton=grand,k=molvi","attribute":[{"name":"firstname","value":["Lucas"]},{"name":"lastname","value":["Brandon"]}]}

4) [{"name":"firstname","value":["Lucas"]},{"name":"lastname","value":["Brandon"]}]

5) ["Lucas"] and ["Brandon"]

1) {"entry":[{"found":"identity=9454532,l=big,ton=grand,k=molvi","attribute":[{"name":"firstname","value":[ "Lucas"]},{"name":"lastname","value":["Brandon"]}]}],"return":{"code":0,"count":1,"message": “成功”}}

2) [{"found":"identity=9454532,l=big,ton=grand,k=molvi","attribute":[{"name":"firstname","value":["Lucas"]} ,{"name":"lastname","value":["Brandon"]}]}]

3) {"found":"identity=9454532,l=big,ton=grand,k=molvi","attribute":[{"name":"firstname","value":["Lucas"]}, {"name":"lastname","value":["Brandon"]}]}

4) [{"name":"firstname","value":["Lucas"]},{"name":"lastname","value":["Brandon"]}]

5) [“卢卡斯”] 和 [“布兰登”]

So basically you have to take care of JSONObject and JSONArray respectively and do the parsing accordingly.

所以基本上你必须分别处理 JSONObject 和 JSONArray 并相应地进行解析。