我有JSON响应
{"as_of":"2013-04-22T19:50:41Z","trends":[{"events":null,
"query":"%23RhymeATweepsName","url":"http:\/\/twitter.com\/search?
q=%23RhymeATweepsName","promoted_content":null,
"name":"#RhymeATweepsName"},
{"events":null,"query":"%23EarthDayPK","url":
"http:\/\/twitter.com\/search?
=%23EarthDayPK","promoted_content":null,"name":
"#EarthDayPK"}],"locations":
[{"woeid":*******,"name":"********"}],"created_at":"2013-04-22T19:38:16Z"}
我正在使用以下代码解析它
jArray = new JSONArray(result);
JSONObject post = null;
for (int ii = 0; ii < jArray.length(); ii++) {
post = jArray.getJSONObject(ii);
String name = post.getJSONObject("trends").getString("name") + "\n";
}
}
但它抛出异常“JSONArray无法转换为JSONObject错误”
答案 0 :(得分:4)
如您的JSON所示,trends
不是JSONObject
,而是JSONArray
:
"trends":[
{
"events":null,
"query":"%23RhymeATweepsName",
"url":"http:\/\/twitter.com\/search? q=%23RhymeATweepsName",
"promoted_content":null,
"name":"#RhymeATweepsName"
},
{
"events":null,
"query":"%23EarthDayPK",
"url":"http:\/\/twitter.com\/search? =%23EarthDayPK",
"promoted_content":null,
"name":"#EarthDayPK"
}
]
你应该解析它,就像这样:
String name = post.getJSONArray("trends").getJSONObject(0).getString("name");
// or iterate... ^^