Reputation: 862
I have that JSON Response
{"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"}
and i am parsing it with the following code
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";
}
}
But it throwing Exception "JSONArray cannot be converted to JSONObject error"
Upvotes: 0
Views: 187
Reputation: 137282
As seen in your JSON, trends
is not a JSONObject
, but a 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"
}
]
You should parse it pretty much like this:
String name = post.getJSONArray("trends").getJSONObject(0).getString("name");
// or iterate... ^^
Upvotes: 4