我似乎无法得到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