【发布时间】:2023-04-03 10:26:01
【问题描述】:
我不明白,为什么会出现这个错误:
04-24 22:11:51.263: W/System.err(27504): org.json.JSONException: 值
<!--HERE JSON VALUE-->org.json.JSONObject 类型的数据不能 转换为 JSONArray
这是我的代码:
JSONObject getProgile = null;
try {
//get json
getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
//convert array
JSONArray array = getProgile.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject c = array.getJSONObject(i);
//get TAG_CUSTOMER
JSONObject customer = c.getJSONObject("Customer");
pName = customer.getString("name");
pLname = customer.getString("name");
}
更新: 我的 json
{
"status": "success",
"data": {
"Customer": {
"id": "33",
"company_id": "1",
"name": "SDfsdf",
"birthdate": "14.02.1989",
"email": "dsfsdf@sf.ff",
"photo": "/files/clients_photos/33/(null)",
"bonuses": "50",
"created": "2015-02-14 12:22:46",
"modified": "2015-02-14 12:22:46",
"ref_id": null,
"ref_code": "6363696029",
"banned": null,
"ban_reason": null,
"ban_ending": null
},
"CustomerVisit": [],
"CustomerBonus": [
{
"id": "29",
"customer_id": "33",
"user_id": "4",
"product_id": null,
"operation": "plus",
"amount": "50",
"subject": "Загрузка фото при регистрации.",
"remain": null,
"modified": "2015-02-14 12:22:46",
"date": "14.02.2015",
"created": "14.02.2015 12:22"
}
],
"CustomerCar": [
{
"id": "41",
"customer_id": "33",
"car_brand_id": "9",
"car_model_id": "11530",
"year": "2020",
"vin": "sdfsdfsdf",
"photo": "",
"number": "dsfsdf",
"created": "2015-02-14 12:22:46",
"modified": "2015-02-14 12:22:46",
"car_brand_name": "BMW",
"car_model_name": "323"
}
],
"CustomerPhone": [
{
"id": "41",
"customer_id": "33",
"phone": "+380990010222",
"created": "2015-02-14 12:22:46",
"modified": "2015-02-14 12:22:46"
}
],
"Insurance": [],
"Event": [],
"Review": [],
"Reservation": []
}
}
【问题讨论】:
-
你的web请求的返回值不是json数组。打印
CustomHttpClient.executeHttpGet的结果 -
您正在尝试将 JSONObject 转换为 JSONArray。这根本不可能。
-
在此处记录您的 getProgile 对象。
-
键入
"data"的值为{"Customer": ...}。那是一个 JSON 对象。您正在尝试将其转换为数组。这相当于Map<?,?> m = whatever(); Object o = m; List<?> l = (List<?>) o,它也会抛出一个ClassCastException。 -
@Jaec 你遇到了什么?
I have JSONobject -> conver JSONarry - > JSONObject c = array.getJSONObject(i) -> JSONObject customer = c.getJSONObject("Customer")可能