0

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();
}
1
  • just replace the initial and final square bracket if possible. Then it will be a json object Commented Oct 5, 2016 at 6:42

4 Answers 4

6
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();
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can get object by using following code :

jsonArray = jsonObject.getJSONArray("contacts");

for(int i=0; i<jsonArray.size(); i++) {
    JSONObject jobject = jsonArray.getJSONObject(i)
}

Comments

0

Use Gson library (https://github.com/google/gson)

Library support converting into JSON or from JSON to object.

Comments

0
JSONArray array = new JSONArray(json_string);

JSONObject obj = array.getObject("sendingReqDataSet");

jsonArray = obj.getJSONArray("contacts");

2 Comments

While this code snippet may answer the question, it doesn't provide any context to explain how or why. Consider adding a sentence or two to explain your answer.
I even think it doesn't answer the question - as it returns JSONArray from a JSONObject and the question is exactly the opposite.

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.