0

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"

3
  • 2
    trends is an array as the exception states. Commented Apr 22, 2013 at 19:57
  • yes it is. Solution ? Commented Apr 22, 2013 at 20:00
  • @ExtremeProgrammer I guess there's a getJSONArray method somewhere close the getJSONObject method? Commented Apr 22, 2013 at 20:01

1 Answer 1

4

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... ^^
Sign up to request clarification or add additional context in comments.

3 Comments

O yes, then how to parse that ?
You would just have to read the trends as an array, trends = JSONArray("trends") and then iterate through them.
YUP, Got it Thanks for Quick response :Highly Appreciated:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.