gpt4 book ai didi

java - 无法从 jsonobject 中提取 jsonarray

转载 作者:行者123 更新时间:2023-12-02 09:43:31 25 4
gpt4 key购买 nike

我一直在与使用改造的服务器的响应作斗争。

我有来自服务器的 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
} ]
}

我使用下面的类来解析 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 正在打印 JSONObject,但它显示 ... org.json.JSONObject 类型的数据无法转换为 JSONArray

最佳答案

您正尝试从 data 字段中提取数组,而响应包含一个对象。也许您想获取对象data,然后从其中获取数组requests

关于java - 无法从 jsonobject 中提取 jsonarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56857248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com