多一個JSONObjects Android的JSON解析我有一個JSON字符串來自服務器和它看起來像這樣內的JSONObject
{
"categories": {
"0": {
"term_id": "247",
"name": "Content Curation"
},
"1": {
"term_id": "50",
"name": "Content Marketing"
},
"2": {
"term_id": "2",
"name": "Curation"
},
"3": {
"term_id": "246",
"name": "Inbound Marketing"
},
"4": {
"term_id": "47",
"name": "Marketing"
},
"5": {
"term_id": "4",
"name": "News Curation"
},
"6": {
"term_id": "36",
"name": "SEO"
},
"8": {
"term_id": "248",
"name": "Wordpress Content Curation"
}
}
}
我的任務是讓「term_id」和「名稱的值「領域。 我用這個代碼從當前的JSONObject中獲取「categories」對象。
JSONObject jObject = new JSONObject(responceData);
JSONObject categoryObject = jObject.getJSONObject("categories");
JSONArray jarray = new JSONArray("["+categoryObject.toString().substring(1,categoryObject.toString().length() - 1) + "]");
for(int i=0;i<jarray.length();i++)
{
JSONObject jobj = jarray.getJSONObject(i);
String term_id=jobj.getString("term_id");
String name=jobj.getString("name");
}
和categoryObject看起來像這樣
{
"0": {
"term_id": "247",
"name": "Content Curation"
},
"1": {
"term_id": "50",
"name": "Content Marketing"
},
"2": {
"term_id": "2",
"name": "Curation"
},
"3": {
"term_id": "246",
"name": "Inbound Marketing"
},
"4": {
"term_id": "47",
"name": "Marketing"
},
"5": {
"term_id": "4",
"name": "News Curation"
},
"6": {
"term_id": "36",
"name": "SEO"
},
"8": {
"term_id": "248",
"name": "Wordpress Content Curation"
}
}
但在那之後我不知道如何獲取該領域。有沒有辦法從JSONObject獲取所有JSONObject子元素?
如果你有一個源代碼或可以給我一個例子,請與我分享。
的類別應該是數組。 –
您可以發佈您的服務器代碼 – 2014-03-24 10:31:36
@Dhaval我沒有到服務器端的訪問,即時遠程解析它 – Arshak92