简体   繁体   English

无法从jsonobject提取jsonarray

[英]Unable to extract jsonarray from jsonobject

I have been battling with response from server using retrofit. 我一直在与服务器使用改进的响应作斗争。

I have this json response from server 我有来自服务器的json响应

{
    "success": true,
    "message": "Your Requests",
    "data": {
        "requests": [
            {
                "_id": "5d163a5ed2399f8be6d8d867",
                "created": "2019-06-28T16:03:42.463Z",
                "pickupCoordinates": [
                    8.0099,
                    6.0909
                ],
                "destinationCoordinates": [
                    9.088788,
                    3.099403
                ],
                "customerName": "Seun Akinbode",
                "pickupAddress": "Lekki",
                "destinationAddress": "Ajah",
                "accessCode": "3334",
                "busPlate": "DD222RR",
                "flaName": "Simi",
                "flaEmail": "awele@kobo360.com",
                "__v": 0
            } ]
}

I use below class to parse the json but unfortunately, it couldn't extract the array requests into jsonArray 我使用下面的类来解析json,但不幸的是,它无法将数组requests提取到jsonArray中

 public ArrayList<RequestModel> getData(String response)
        {
            Log.d("responseData :", response);
            ArrayList<RequestModel> dataList = new ArrayList<>();
            try {
                JSONObject mainObj = new JSONObject(response);
                JSONArray array = mainObj.getJSONArray("data");
                Log.d("The main Array :", array.toString());
                RequestModel data;
                for(int i = 0;i<array.length();i++)
                {
                    data = new RequestModel();
                    JSONObject resObj = array.getJSONObject(i);
                    JSONArray reqArray = resObj.getJSONArray("requests");
                    for( int j =0;j<reqArray.length();j++) {
                        JSONObject reqObj = reqArray.getJSONObject(j);
                        data.setAccessCode(reqObj.getString("accessCode"));
                        Log.d("Accessecode :", reqObj.getString("accessCode"));
                        data.setCustomerName(reqObj.getString("customerName"));
                        Log.d("customerName :", reqObj.getString("customerName"));
                    }

                    dataList.add(data);


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

            return dataList;
        }

Logcat is printing the JSONObject but it says at ... data of type org.json.JSONObject cannot be converted to JSONArray Logcat正在打印JSONObject,但是它说在... data of type org.json.JSONObject cannot be converted to JSONArray

You are trying to extract an array from the data field, while the response contains an object. 您正在尝试从data字段中提取数组,而响应包含一个对象。 Perhaps you meant to get the object data , then the array requests from within it. 也许您打算获取对象data ,然后从其中requests数组。

This could be the problem: 这可能是问题所在:

JSONArray array = mainObj.getJSONArray("data");

data is an object as seen in the response, not an array. 数据是响应中看到的对象,而不是数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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