2016-05-12 240 views
0

我想解析多級JSON(來自Quandl),但我有困難解析它正確。解析多層JSON

我無法弄清楚如何解析的部分在數據標籤下。如何選擇它並遍歷所有值,因爲它們不是密鑰對。

這部分代碼需要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

爲什麼你解析一個JSONObject成JSONArray在第一行?我認爲這是你的問題... – Jim

回答

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)); 
     } 
    } 
+0

感謝您的答覆,但我得到的數據類型不匹配stockArray.getJSONArray(「數據」)期望int 'JSONObject stockObject = new JSONObject(result); JSONArray stockArray = stockObject.getJSONArray(「dataset」); JSONArray statsArray = stockArray.getJSONArray(「data」);對於(int i = 0; i EagleUK

+0

@EagleUK立即查看 –