简体   繁体   中英

Unable to convert JSONObject into JSONArray

I am calling a REST API using the android app, the results of the POST method API is as follows:

{
"Items": {
    "ap_id": "37",
    "ap_time_from": "14:28",
    "ap_time_to": "16:28",
    "patient_id": "153",
    "patient_name": "Nikhil",
    "patient_email": "a@a.com",
    "patient_location": "abc"
}                        }

Converting it into readable data using:

JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("Items");

for (int i = 0; i < jsonArray.length(); i++) {

 JSONObject object = jsonArray.getJSONObject(i);

DoctorModel doctorModel1 = new DoctorModel();
doctorModel1.setApp_id(object.getString("ap_id"));
doctorModel1.setStart_timing(object.getString("ap_time_from"));
doctorModel1.setEnd_timing(object.getString("ap_time_to"));
doctorModel1.setUser_id(object.getString("patient_id"));
doctorModel1.setUser_name(object.getString("patient_name"));
doctorModel1.setUser_mail(object.getString("patient_email"));
doctorModel1.setLocation(object.getString("patient_location"));
doctorModelList.add(doctorModel1); }

Now when I am trying to convert it so that I can display the results in a recycler view I am getting the following error:

org.json.JSONException: Value
{"ap_id":"37","ap_time_from":"14:28","ap_time_to":"16:28","patient_id":"153","patient_name":"Nikhil","patient_email":"a@a.com","patient_location":"abc"}
at Items of type org.json.JSONObject cannot be converted to JSONArray

I have been using the same way of converting the JSON Object data into JSON Array, not sure where I am going wrong. Any help would be appreciated, thanks!

You need to have an array as an object inside Items , as follows :

{
"Items": 
    [
        {
        "ap_id": "37",
        "ap_time_from": "14:28",
        "ap_time_to": "16:28",
        "patient_id": "153",
        "patient_name": "Nikhil",
        "patient_email": "a@a.com",
        "patient_location": "abc"
        }
    ]                        
}

Else, change your code to support single object instead of an array

JSONObject jsonObject = new JSONObject(response);
JSONArray object = jsonObject.JSONObject("Items"); 

In the current response, the value of Items has a JSON object, not a JSON array. That's why this exception happens. To convert this response to data model then you can use this:

JSONObject jsonObject = new JSONObject(response);
JSONArray object = jsonObject.JSONObject("Items"); 

DoctorModel doctorModel1 = new DoctorModel();
doctorModel1.setApp_id(object.getString("ap_id"));
doctorModel1.setStart_timing(object.getString("ap_time_from"));
doctorModel1.setEnd_timing(object.getString("ap_time_to"));
doctorModel1.setUser_id(object.getString("patient_id"));
doctorModel1.setUser_name(object.getString("patient_name"));
doctorModel1.setUser_mail(object.getString("patient_email"));
doctorModel1.setLocation(object.getString("patient_location"));
doctorModelList.add(doctorModel1);

Or if you want to get the array from json then the JSON will look like this:

{
"Items": 
    [
        {
        "ap_id": "37",
        "ap_time_from": "14:28",
        "ap_time_to": "16:28",
        "patient_id": "153",
        "patient_name": "Nikhil",
        "patient_email": "a@a.com",
        "patient_location": "abc"
        }
    ]                        
}

Hope you understand the isse.

JSONArray jsonArray = jsonObject.getJSONArray("Items"); // this line getting exception because you are trying to convert JSONObject("Items") into JSONArray.

you have to make some changes in the response: 1. If you want to return "Items" as Array in Response.

{
"Items": [
    {
      "ap_id": "37",
      "ap_time_from": "14:28",
      "ap_time_to": "16:28",
      "patient_id": "153",
      "patient_name": "Nikhil",
      "patient_email": "a@a.com",
      "patient_location": "abc"
    }
  ]
}
  1. If you want to return "Items" like an object in Response then you have to change in code like this

JSONObject jsonObject = new JSONObject(response); JSONObject jsonObjectItem = jsonObject.getJSONObject("Items");

But in the second case, you can not get the result in the form of an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM