使用JSON Java检索嵌套数组值 [英] Retrieving nested arrays values with JSON Java

查看:92
本文介绍了使用JSON Java检索嵌套数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

{
  "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
    }
  }
}

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

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:

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!

推荐答案

使用的库:

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:姓名 值:[ 卢卡斯]},{ 名称: 姓, 值:[ 布兰登]}]}], 返回:{ 代码:0,计:1,消息:成功}}

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]}]}]

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

3){找到: 同一性= 9454532,L =大,吨=隆重,K = molvi, 属性:[{ 名称: 姓名, 值:[ 卢卡斯]},{ 名称: 姓氏,价值:[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]}]

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

5)[Lucas]和[Brandon ]

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

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

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

这篇关于使用JSON Java检索嵌套数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆