-4

I'm parsing this JSON on android -

{
  "data": {
    "is_silhouette": false,
    "url": "https://scontent.xx.fbcdn.net/hphotos-xat1/v/t1.0-9/s180x540/34325347_936407749733967_1354847545689012266_n.jpg?oh=dde033205b1230568dc26b7b01cy5424&oe=56599FD6"
  }
}

I have this code -

                        json = response.getJSONObject();
                        Log.d("json data",json.toString());

                        try {

                            jarray = json.getJSONArray("data");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

that gives this error - data of type org.json.JSONObject cannot be converted to JSONArray

Can anyone tell what wrong is there ? Any pointer is appreciated.

5
  • Also post your JSON returned data. It seems like "data" is not array. Commented Jul 30, 2015 at 12:29
  • You get perhaps a JSONObject and inside your JSONArray, it's better to post your Json data. Commented Jul 30, 2015 at 12:31
  • i just updated the question with the JSON Commented Jul 30, 2015 at 12:31
  • data is not a jsonArray , it is a jsonObject Commented Jul 30, 2015 at 12:34
  • How should the correction be made in the code ? Commented Jul 30, 2015 at 12:35

5 Answers 5

3

JSON Array start with [ and end with ] : enter image description here

and JSON Object start with { and end with } : enter image description here

So you need to get JSONObject not JSONArray. getJSONObject("data")

Sign up to request clarification or add additional context in comments.

Comments

2

The data instance isn't an array, but a JSON object. Therefore getJSONArray throws the exception. Use getJSONObject("data") instead.

Comments

1

Take a look here on examples of JSON data types: http://www.w3schools.com/json/json_syntax.asp

You are working with object, not array. Arrays are rounded with '[' and ']'.

Comments

1

As "data" is a JsonObject and you are trying to access it as JsonArray you are facing this exception instead try this:

json = response.getJSONObject();
Log.d("json data",json.toString());

try {
    JSONObject jobject = json.getJSONObject("data");
} catch (JSONException e) {
    e.printStackTrace();
}

Comments

1

There is no JSON array in your json. Use following code

String jsonString = "{
  'data': {
    'is_silhouette': false,
    'url': 'https://scontent.xx.fbcdn.net/hphotos-xat1/v/t1.0-9/s180x540/34325347_936407749733967_1354847545689012266_n.jpg?oh=dde033205b1230568dc26b7b01cy5424&oe=56599FD6'
  }
}";
JSONObject jsonObj =  new JSONObject(jsonString);

JSONObject data  =  jsonObj.getJSONObject("data");
String url = data.getString("url");

Comments

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.