我有一个来自服务器的 JSON 字符串,它看起来像这样:
{
"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”和“name”字段的值。
我曾经使用以下代码从当前 JSONObject 获取“类别”对象:
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");
}
and the 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 子级?
如果您有源代码或可以给我一个示例,请与我分享。