简体   繁体   English

如何使用Java代码从具有嵌套数据的Json数组中获取值

[英]How to fetch the value from Json array having nested data using Java code

I have to fetch the value from array "machines" having nested data and need to fetch for "value1": "Need to fetch this value". 我必须从具有嵌套数据的数组“ machines”中获取值,并且需要获取“ value1”:“需要获取此值”。 Tried a lot of code but still not able to fetch the same. 尝试了很多代码,但仍然无法获取相同的代码。

{
   "name":"anyname",
   "machines":[
      {
         "id":"771760",
         "type":"general",
         "properties":{
            "value1":"1",
            "value2":"2"
         }
      },
      {
         "id":"341256",
         "type":"general",
         "properties":{
            "value1":"Need to fetch this value"
         }
      },
      {
         "id":"341256",
         "type":"general",
         "properties":{
            "value1":"1",
            "value2":"2"
         }
      }
   ]
}

Tried using JsonObject and JsonArray, Still not worked 使用JsonObject和JsonArray尝试过,仍然无法正常工作

public String getValueForAnyKeyHavingNestedObjects(String jsonData,String outerObjectKey, String keyWhoseValueToFetch) throws JSONException {

JSONObject obj = new JSONObject(jsonData);

String value = String.valueOf(obj.getJSONObject(outerObjectKey).get(keyWhoseValueToFetch));
return value;
}

So you have your jsonData which you need to put in a JSONObject . 因此,您需要将JSONData放入JSONObject

You need to extract the machines, which is an array using getJSONArray("machines") . 您需要提取机器,这是使用getJSONArray("machines")的数组。

After that, you want to iterate over each machine and convert the machine into another JSONObject . 之后,您要遍历每台machine并将该machine转换为另一个JSONObject

In order to take the value1, you just do a normal get("value1") . 为了获取value1,您只需执行常规的get("value1")

Full example here: 完整示例如下:

public static void main(String[] args) {
        String jsonData = "{\n"
                + "   \"name\":\"anyname\",\n"
                + "   \"machines\":[\n"
                + "      {\n"
                + "         \"id\":\"771760\",\n"
                + "         \"type\":\"general\",\n"
                + "         \"properties\":{\n"
                + "            \"value1\":\"1\",\n"
                + "            \"value2\":\"2\"\n"
                + "         }\n"
                + "      },\n"
                + "      {\n"
                + "         \"id\":\"341256\",\n"
                + "         \"type\":\"general\",\n"
                + "         \"properties\":{\n"
                + "            \"value1\":\"Need to fetch this value\"\n"
                + "         }\n"
                + "      },\n"
                + "      {\n"
                + "         \"id\":\"341256\",\n"
                + "         \"type\":\"general\",\n"
                + "         \"properties\":{\n"
                + "            \"value1\":\"1\",\n"
                + "            \"value2\":\"2\"\n"
                + "         }\n"
                + "      }\n"
                + "   ]\n"
                + "}";

        final JSONObject jsonObject = new JSONObject(jsonData);
        final JSONArray machines = jsonObject.getJSONArray("machines");
        for (int i = 0; i < machines.length(); i++) {
            final JSONObject machine = machines.getJSONObject(i);
            final JSONObject properties = machine.getJSONObject("properties");
            System.out.println(properties.get("value1"));
        }
    }

Result: 结果:

1 1个

Need to fetch this value 需要获取此值

1 1个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM