views:

290

answers:

1

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?

+1  A: 

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...

Marc Novakowski
OK. I'm still confused.There are lots of elements (JSONObjects) in jsoarray.Let's say I want to access the first element: JSONObject foo = jsoarray.getJSONObject(0);foo has lots of keys: profile_image_url, created_at, tweeter_id, etc. I'd like to know what value the "profile_image_url" has, so I do this: tweeter_profile_image_url = foo.getString("profile_image_url")And it still crashes. Hmm...
anta40
If there's a chance that a key in the JSONObject may not exist, use optString instead of getString otherwise it will throw a JSONException.
Marc Novakowski
Just tried this:tweeter_profile_image_url = foo.optString("profile_image_url");And it still crashes.But when I try this:Dialog.inform("foo.length(): " + foo.length());The message box will says 11, which is correct.
anta40
Finally, I found the culprit. It's not the parsing itself (the parsing went find), but due to "synchronized (UiApplication.getEventLock())".I forgot to put that thing in my code.Now the problem is solved. Many thanks, anyway.
anta40