我正试图让json数组的内部对象(我的行话可能不正确)看起来像这样:
{
"News": {
"0": {
"name": "nytimes",
"fullName": "The New York Times",
"text": "@OwenPerry We heard from the owner of the house and corrected the article http://t.co/8GEgGy7",
"timestamp": "2011-08-15 12:20:36"
},
"1": {
"name": "HuffingtonPost",
"fullName": "Huffington Post",
"text": "Look out! New Zealand Skier chased by angry bulls - http://t.co/L0PZkx4",
"timestamp": "2011-08-15 12:19:04"
}
}
}
我检索内部对象的代码如下所示:
JSONArray category = json.getJSONArray("News");
JSONArray innerobj = category.getJSONArray(0);
for (int i = 0; i < innerobj.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = innerobj.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", c.getString("fullName") + "\n(#"
+ c.getString("name") + ") ");
map.put("text",
c.getString("text") + "\n - "
+ c.getString("timestamp"));
mylist.add(map);
}
关于我做错了什么的想法?
答案 0 :(得分:3)
您没有JSONArray
,而是JSONObjects
。您的JSONObject
结构是:
JSONObject
,其中包含1 JSONObject
- “新闻”JSONObjects
,“0”和“1”考虑:
JSONObject jobj = new JSONObject(yourString);
JSONObject newsobj = jobj.getJSONObject("news");
JSONObject firstOne = newsobj.getJSONObject("0"); // this is the object "0"
....