我似乎無法得到JSON數組中的第二層。 我想要的是,從類別中獲取所有名稱,然後我想通過意圖將「論壇ID」發送到不同的活動。
如果有幫助,我跟着這個tutorial,我讓我的JSON數組從here我如何獲得已經在JSONArray中的JSONArray
部分JSON陣列的
{
"categories": [{
"name": "Facepunch",
"forums": [{
"id": 6,
"name": "General Discussion",
"viewing": 217
}, {
"id": 60,
"name": "Fast Threads",
"viewing": 188
}, {
"id": 64,
"name": "Videos And Flash Movies and That Kind Of Crap",
"viewing": 239
}, {
"id": 403,
"name": "Mass Debate",
"viewing": 9
}, {
"id": 396,
"name": "Sensationalist Headlines",
"viewing": 455
}, {
"id": 51,
"name": "In The News Node",
"viewing": 100
}]
}]
}
我的代碼部分
try {
// Getting Array of Contacts
categories = json.getJSONArray(TAG_CATEGORIES);
forums = json.getJSONArray(TAG_FORUMS);
// looping through All categories
for(int i = 0; i < categories.length(); i++){
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String CatName = c.getString(TAG_CATName);
// Phone number is agin JSON Object
JSONObject All_forums = forums.getJSONObject(i);
String forum_id = All_forums.getString(TAG_FORUMS_ID);
String forum_name = All_forums.getString(TAG_FORUMS_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CATName, CatName);
map.put(TAG_FORUMS, forum_name);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
當我作出這樣的循環,它似乎只輸出JSON數組(稱爲嗜好)中的最後一項。即使是第一個循環內部的循環。 – dasmikko 2013-02-18 14:59:17
也許你正在以錯誤的方式存儲值?嘗試通過在內部循環內添加一個* System.out.println(forum_name)*來進行調試。 – nutlike 2013-02-18 15:05:56
我顯然保持覆蓋鍵「名字」當我做內循環,當我映射HashMap中的鍵。 – dasmikko 2013-02-18 15:58:22