【发布时间】:2020-02-06 12:37:20
【问题描述】:
在我的项目中,我正在尝试获取数据,数据位于数组中,因此我执行了所需的步骤,但数组在数据中有一个名称,我不知道如何传递 "listCar"[]。
这是我的获取代码:
private void getData() {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
CarClass car = new CarClass();
car.setTitle(jsonObject.getString("adTitle"));
car.setDate(jsonObject.getString("adDate"));
car.setPrice(jsonObject.getDouble("adPrice"));
car.setCategory(jsonObject.getString("category"));
car.setBrand(jsonObject.getString("brand"));
car.setBrandModel(jsonObject.getString("brandModel"));
car.setKilometer(jsonObject.getDouble("kilometer"));
car.setModelYear(jsonObject.getString("modelYear"));
car.setFuel(jsonObject.getString("fuelType"));
car.setGear(jsonObject.getString("gearType"));
carClassList.add(car);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
这是我得到的日志错误:
E/Volley: com.android.volley.ParseError: org.json.JSONException: Value org.json.JSONObject 类型的无法转换为 JSONArray
回复:
{
"listCar": [
{
"id": 204,
"adTitle": null,
"adDate": "2019-11-07T11:52:40.0156875",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 203,
"adTitle": null,
"adDate": "2019-11-07T11:52:37.7771547",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 202,
"adTitle": null,
"adDate": "2019-11-07T11:52:35.5569602",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 201,
"adTitle": null,
"adDate": "2019-11-07T11:52:33.2120309",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 200,
"adTitle": null,
"adDate": "2019-11-07T11:52:30.8982098",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 199,
"adTitle": null,
"adDate": "2019-11-07T11:52:15.1157196",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 198,
"adTitle": null,
"adDate": "2019-11-07T11:50:09.5922072",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 197,
"adTitle": null,
"adDate": "2019-11-07T11:44:08.8795626",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 196,
"adTitle": null,
"adDate": "2019-11-07T11:42:40.3744716",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 195,
"adTitle": null,
"adDate": "2019-11-07T09:34:43.119591",
"adPrice": 2400,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 5,
"brandModel": "M500",
"kilometer": 170000,
"modelYearId": 4,
"modelYear": "2012",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "normal",
"carFeatureGearId": 1,
"image": {
"path": "Upload/UploadCarMain/UploadCarMain-200-200/30032235992008220498istanbul_adalarCar195.jpg",
"name": "30032235992008220498istanbul_adalarCar195.jpg"
},
"vendorId": 14
}
],
"count": 74,
"responseStatusCode": 1,
"descriptionResponse": "Success"
}
如何将“listCar”传递给我的响应以访问数据?
【问题讨论】:
-
您的
JSONArray,listCar[]它自我包装在JSONObject中,这就是您收到错误的原因 -
如何解决有什么解决方案? @AshwiniViolet
-
查看@Md.Asaduzzaman 提供的答案
标签: java android json android-volley