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

Gissipi_453
  • 1,250
  • 1
  • 25
  • 61

5 Answers5

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")

Karan Rana
  • 638
  • 4
  • 14
2

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

user2810895
  • 1,284
  • 3
  • 19
  • 33
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 ']'.

sphinks
  • 3,048
  • 8
  • 39
  • 55
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();
}
sphinks
  • 3,048
  • 8
  • 39
  • 55
manjusha
  • 11
  • 2
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");
sphinks
  • 3,048
  • 8
  • 39
  • 55
Ketan
  • 423
  • 3
  • 12