解析多层次的json

时间:2016-05-12 00:39:12

标签: java android json parsing android-studio

我正在尝试解析多级JSON(来自Quandl),但我在解析它时遇到了困难。

我无法弄清楚如何解析的部分是在数据标记下。如何选择它并迭代所有值,因为它们不是密钥对。

这部分代码需要一个int

stockArray.getJSONArray("data");

非常感谢任何帮助。

这就是我现在正在尝试的方法:

 JSONObject stockObject = new JSONObject(result);
            JSONArray stockArray = stockObject.getJSONArray("dataset");
            JSONArray statsArray = stockArray.getJSONArray("data");
            for(int i = 0; i < statsArray.length(); i++){
                JSONArray arr = statsArray.getJSONArray(i);
                for(int j = 0; j < arr.length(); j++){
                    Log.d("TAG", arr.getString(j));
                }
            }

这是json:

{  
   "dataset":{  
      "id":9792521,
      "dataset_code":"KIBO",
      "database_code":"LSE",
      "name":"KIBO MINING share price (KIBO), Currency GBX",
      "description":"Stock Prices for Kibo Mining Share Price (kibo), Currency Gbx from the London Stock Exchange.\u003cbr\u003e\u003cbr\u003eCurrency: GBX",
      "refreshed_at":"2016-05-11T21:52:53.568Z",
      "newest_available_date":"2016-05-11",
      "oldest_available_date":"2010-04-27",
      "column_names":[  
         "Date",
         "Price",
         "High",
         "Low",
         "Volume",
         "Last Close",
         "Change",
         "Var%"
      ],
      "frequency":"daily",
      "type":"Time Series",
      "premium":false,
      "limit":10,
      "transform":null,
      "column_index":null,
      "start_date":"2010-04-27",
      "end_date":"2016-05-11",
      "data":[  
         [  
            "2016-05-11",
            4.0,
            4.25,
            3.88,
            434000.0,
            4.0,
            0.0,
            0.0
         ],
         [  
            "2016-05-10",
            4.0,
            4.25,
            3.88,
            1611062.0,
            4.0,
            0.0,
            0.0
         ],
         [  
            "2016-05-09",
            4.0,
            4.25,
            4.0,
            2005382.0,
            4.0,
            -0.25,
            -5.88
         ],
         [  
            "2016-05-06",
            4.25,
            4.5,
            4.12,
            743687.0,
            4.25,
            0.12,
            3.03
         ],
         [  
            "2016-05-05",
            4.12,
            5.12,
            4.12,
            5731469.0,
            4.12,
            -1.0,
            -19.51
         ],
         [  
            "2016-05-04",
            5.12,
            6.25,
            4.88,
            5348475.0,
            5.12,
            -0.62,
            -10.87
         ],
         [  
            "2016-05-03",
            5.75,
            6.0,
            5.38,
            4313650.0,
            5.75,
            0.38,
            6.98
         ],
         [  
            "2016-04-29",
            5.38,
            5.5,
            5.0,
            3654596.0,
            5.38,
            0.25,
            4.88
         ],
         [  
            "2016-04-28",
            5.12,
            5.38,
            4.75,
            2298599.0,
            5.12,
            0.25,
            5.13
         ],
         [  
            "2016-04-27",
            4.88,
            4.88,
            4.25,
            3586011.0,
            4.88,
            0.62,
            14.71
         ]
      ],
      "collapse":null,
      "order":"desc",
      "database_id":384
   }
}

1 个答案:

答案 0 :(得分:0)

您在数据数组中有另一个数组。

此代码将记录每个元素:

   JSONObject rootObject = new JSONObject(result);
   JSONObject stockObject = rootObject.getJSONObject("dataset");
   JSONArray statsArray = stockObject.getJSONArray("data");
   for(int i = 0; i < statsArray.length(); i++){
       JSONArray arr = statsArray.getJSONArray(i);
       for(int j = 0; j < arr.length(); j++){
           Log.d("TAG", arr.getString(j));
       }
   }