5

I have a JSON format like this

{"response":{"status":true,"result":"user_exists"}}

Now i am trying to retrieve the Status value to do some logic

JSONObject jData = new JSONObject(data);
JSONArray response = jData.getJSONArray("response");

But i am getting the following error

org.json.JSONException: Value {"result":"user_exists","status":true} at response of type org.json.JSONObject cannot be converted to JSONArray

how to retrieve an Object from inside and Object ?

Houcine
24.2k13 gold badges59 silver badges84 bronze badges
asked Nov 28, 2011 at 9:51

4 Answers 4

5

response is a JSONObject, not a JSONArray. Array objects are surrounded by these [] brackets, objects are with the normal ones {}. (See json.org for more format information)

Change

JSONArray response = jData.getJSONArray("response");

to

JSONObject response = jData.getJSONObject("response");
answered Nov 28, 2011 at 9:54
Sign up to request clarification or add additional context in comments.

Comments

4

you are trying to retreive the status attribut from a JSONArray but , you don't have any JSONArray in your Code , ( JSONArray is surrounded by [] , and JSONObject is surrounded by {} ) , So to retreive the status value , try this :

JSONObject jData = new JSONObject(data);
JSONObject response = jData.getJSONObject("response");
boolean status = response.getBoolean("status");
answered Nov 28, 2011 at 10:05

Comments

1

response isn't an array but an object. Use getJSONObject and JSONObject instead of getJSONArray and JSONArray.

answered Nov 28, 2011 at 9:54

Comments

1

You have to first navigate to the response object by

JSONObject response = jData.getJSONObject("response") instead of JSONArray, as response is a object.

Reno
33.8k11 gold badges95 silver badges105 bronze badges
answered Nov 28, 2011 at 9:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.