简体   繁体   English

如何在JSON数组中提取JSON数组

[英]How to extract an array of JSON inside JSON array

{
  "hits": [
    {
      "name": "Google",
      "results": [
        {
          "count": 27495
        }
      ]
    },
    {
      "name": "Yahoo",
      "results": [
        {
          "count": 17707
        }
      ]
    }
}

i'am able to read the name and results from the above json by the below code, but unable to print the count value alone from JSON. 我能够通过以下代码从上述json读取名称和结果,但无法单独从JSON打印计数值。

JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
if(null!=jsonObject.get("hits"))
{
    System.out.println("Inside IF...");
    JSONArray ja = (JSONArray) jsonObject.get("hits");
    for(int i=0;i<ja.size() ; i++)
    {
        System.out.println("Inside FOR...");
        JSONObject tempJsonObj = (JSONObject) ja.get(i);
        System.out.println(tempJsonObj.get("name").toString());
        System.out.println(tempJsonObj.get("results").toString());
    }
}

How to extract an array of JSON inside JSON array 如何在JSON数组中提取JSON数组

Just as you parsed for Outer JSONArray (hits ) . 就像您解析外部JSONArray(hits)一样。 Follow the same for inner ("results") : 对于内部(“结果”)遵循相同的步骤:

JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
    if(null!=jsonObject.get("hits"))
    {
        System.out.println("Inside IF...");
        JSONArray ja = (JSONArray) jsonObject.get("hits");
        for(int i=0;i<ja.size() ; i++)
        {
            System.out.println("Inside FOR...");
            JSONObject tempJsonObj = (JSONObject) ja.get(i);
            System.out.println(tempJsonObj.get("name").toString());
            System.out.println(tempJsonObj.get("results").toString());
      JSONArray innerarray = (JSONArray) tempJsonObj.get("results");
    for(int i=0;i<innerarray.size() ; i++)
    {
     JSONObject tempJsoninnerObj = (JSONObject) innerarray.get(i);
            System.out.println(tempJsoninnerObj.get("count").toString());
    }


        }
    }

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

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