2016-07-14 116 views
2

所以創建一個ArrayList我有一個JSON就像下面存儲MYJSON圖片 -從JSON對象

enter image description here

我的計劃是從childObject0檢索所有的對象,並將其存儲在一個ArrayList所以他們可以稍後處理。所以我沒有 -

ArrayList<String> childLists = new ArrayList<String>(); 
try { 
     JSONArray childArray = MYJSON.getJSONArray("childObject0"); 
     for (int i = 0; i<jArray.length(); i++ 
       ) { 
      //I lost it here! How can I append to `childList` from `childArray`? 

      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

無法弄清楚如何追加。這是正確的方法嗎? childObject0是動態的,計數會隨時變化。 謝謝

回答

2

由於childObject0中的每個對象都是json,因此可以將其存儲爲ArrayList<JSONObject>。這應該使對象比字符串更容易處理。

ArrayList<JSONObject> childList = new ArrayList<JSONObject>(); 
try { 
    JSONArray childArray = MYJSON.getJSONArray("childObject0"); 
    for (int i = 0; i < childArray.length(); i++) { 
     childList.add(childArray.getJSONObject(i)); 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
+0

使用'ArrayList '保存了一天! – envyM6