Reputation: 6743
I'm working on how to parse Twitter's JSON response using JSON-ME.
For example:
http://apiwiki.twitter.com/Twitter-Search-API-Method:-search
foo({"results":[{"profile_image_url":"http://a3.twimg.com/profile_images/762620209/drama_queen-6989_normal.gif","created_at":"Thu, 01 Apr 2010 02:35:10 +0000","from_user":"TWEETSDRAMA","to_user_id":null,"text":"NEW Twitter Lists Widget - How to put it on your blog or site http://bit.ly/47NCi6","id":11401539152,"from_user_id":95081097,"geo":null,"iso_language_code":"en","source":"<a href="http://ping.fm/" rel="nofollow">Ping.fm</a>"}... (content truncated)
Here's my method:
public void parseDataFromJSON(String strjson) throws JSONException {
JSONTokener jtoken = new JSONTokener(strjson);
JSONArray jsoarray = new JSONArray(jtoken);
JSONObject jsobj = jsoarray.getJSONObject(0);
tweeter_profile_image_url = jsobj.optString("profile_image_url");
tweeter_created_at = jsobj.optString("created_at");
tweeter_from_user = jsobj.optString("from_user");
tweeter_to_user_id = jsobj.optString("to_user_id");
tweeter_text = jsobj.optString("text");
tweeter_id = jsobj.optInt("id");
tweeter_from_user_id = jsobj.optInt("from_user_id");
tweeter_geo = jsobj.optString("geo");
tweeter_iso_language_code = jsobj.optString("iso_language_code");
tweeter_source = jsobj.optString("source")
}
When I ran it on the emulator, nothing was shown, so I inspected the debugger, and the output was: status: 200 content: {"results":[{"profile_image_url":"http://a1.twimg.com/profile_images/746683548/Photo_on_2010-..... ---> OK, I got the JSON content
org.json.me.JSONException: A JSONArray text must start with '[' at character 1 of {"results":[{"profile_image_url.... ---> but somehow unable to processed it properly.
So how to parse this correctly?
Upvotes: 0
Views: 1656
Reputation: 45398
The response itself is an object, so you only need this to parse it:
JSONObject jsobj = new JSONObject(strjson);
It's an object with a single key, "result", which is itself an array. So you'd do something like this:
JSONArray jsoarray = jsobj.getJSONArray("results");
Then each element in the "jsoarray" array will be a JSONObject, etc. etc. You can see how the different JSON objects are nested within each other...
Upvotes: 2