androidjsonandroid-json

How to convert JSON array into JSON object


I have the following JSON data:

[
    {
        "sendingReqDataSet": {
            "totalCount": 1,
            "limit": 50,
            "offset": 0,
            "status_code": true,
            "contacts": [
                {
                    "id": 1,
                    "request_to": "Shehla_kiran",
                    "request_by": "ayesha",
                    "product_name": "Dead Space",
                    "schedule_date_time": "2016-09-21 00:00:00",
                    "place": "lhr",
                    "request_type": "Swap",
                    "status": "Pending"
                },
                {
                    "id": 2,
                    "request_to": "muqadas",
                    "request_by": "muqadas",
                    "product_name": "battleField",
                    "schedule_date_time": "2016-09-22 00:00:00",
                    "place": "lhr",
                    "request_type": "Swap",
                    "status": "Pending"
                },
                {
                    "id": 3,
                    "request_to": "Shehla_kiran",
                    "request_by": "Shehla_kiran",
                    "product_name": "Mario",
                    "schedule_date_time": "2016-09-12 00:00:00",
                    "place": "Lahore",
                    "request_type": "Swap",
                    "status": "Pending"
                },
                {
                    "id": 4,
                    "request_to": "Shehla_kiran",
                    "request_by": "Mari",
                    "product_name": "Mario",
                    "schedule_date_time": "0000-00-00 00:00:00",
                    "place": "",
                    "request_type": "Swap",
                    "status": "Pending"
                },
                {
                    "id": 5,
                    "request_to": "Shehla_kiran",
                    "request_by": "Faisal Ali",
                    "product_name": "Dead Space",
                    "schedule_date_time": "2016-10-28 00:00:00",
                    "place": "lahore",
                    "request_type": "Swap",
                    "status": "Pending"
                },
                {
                    "id": 6,
                    "request_to": "muqadas",
                    "request_by": "Faisal Ali",
                    "product_name": "battleField",
                    "schedule_date_time": "2016-10-25 00:00:00",
                    "place": "Canal",
                    "request_type": "Swap",
                    "status": "Pending"
                },
                {
                    "id": 7,
                    "request_to": "Shehla_kiran",
                    "request_by": "Shehla_kiran",
                    "product_name": "Dead Space",
                    "schedule_date_time": "2016-10-17 00:00:00",
                    "place": "Lahore",
                    "request_type": "Swap",
                    "status": "Pending"
                }
            ]
        }
    }
]

I am trying to show it in a list view, but when I try to parse it and show in a list view, it gives me the following error:

W/System.err: org.json.JSONException: 
Value [{"sendingReqDataSet":{"totalCount":1,"limit":50,"offset":0,"status_code":true,"contacts":[{"id":1,"request_to":"Shehla_kiran","request_by":"ayesha","product_name":"Dead Space","schedule_date_time":"2016-09-21 00:00:00","place":"lhr","request_type":"Swap","status":"Pending"},{"id":2,"request_to":"muqadas","request_by":"muqadas","product_name":"battleField","schedule_date_time":"2016-09-22 00:00:00","place":"lhr","request_type":"Swap","status":"Pending"},{"id":3,"request_to":"Shehla_kiran","request_by":"Shehla_kiran","product_name":"Mario","schedule_date_time":"2016-09-12 00:00:00","place":"Lahore","request_type":"Swap","status":"Pending"},{"id":4,"request_to":"Shehla_kiran","request_by":"Mari","product_name":"Mario","schedule_date_time":"0000-00-00 00:00:00","place":"","request_type":"Swap","status":"Pending"},{"id":5,"request_to":"Shehla_kiran","request_by":"Faisal Ali","product_name":"Dead Space","schedule_date_time":"2016-10-28 00:00:00","place":"lahore","request_type":"Swap","status":"Pending"},{"id":6,"request_to":"muqadas","request_by":"Faisal Ali","product_name":"battleField","schedule_date_time":"2016-10-25 00:00:00","place":"Canal","request_type":"Swap","status":"Pending"},{"id":7,"request_to":"Shehla_kiran","request_by":"Shehla_kiran","product_name":"Dead Space","schedule_date_time":"2016-10-17 00:00:00","place":"Lahore","request_type":"Swap","status":"Pending"}]}}] 
of type org.json.JSONArray cannot be converted to JSONObject

Currently, I am doing this:

try {
    // jsonObject = new JSONObject();
    JSONObject jsonObject = new JSONObject(json_string);
    // JSONArray query = jsonParse.getJSONArray("courses");

    jsonArray = jsonObject.getJSONArray("contacts");

    int count = 0;
    String name, email, moblile;

    while (count < jsonArray.length()) {
        JSONObject JO = jsonArray.getJSONObject(count);
        
        name    = JO.getString("request_to");
        email   = JO.getString("request_by");
        moblile = JO.getString("product_name");
        
        Contacts contacts = new Contacts(name, email, moblile);
        contactAdapter.add(contacts);
        count++;
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Solution

  • String data = /*load the json data*/
    try {
        JSONArray jsonArray = new JSONArray(data);
        JSONObject object = jsonArray.getJSONObject(0);
        JSONObject sendingReqDataSetObject = object.getJSONObject("sendingReqDataSet");
        JSONArray arrayContacts = sendingReqDataSetObject.getJSONArray("contacts");
        
        for (int i = 0; i<arrayContacts.length(); i++) {
            JSONObject contactObject = arrayContacts.getJSONObject(i);
            System.out.println(contactObject.getString("status"));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }