如何访问数组名称以获取列表?
How can I access the array name in order to get list?
在我的项目中,我正在尝试获取数据,数据在数组中,所以我执行了所需的步骤,但数组在数据中有一个我不知道如何传递的名称"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
of type org.json.JSONObject cannot be converted to 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" 传递给我的响应以便访问数据?
您必须使用 JsonObjectRequest
而不是 JsonArrayRequest
因为您的回复实际上是 JSONObject
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("listCar");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
CarClass car = new CarClass();
car.setTitle(jsonObject.getString("adTitle"));
car.setDate(jsonObject.getString("adDate"));
// Left of the implementation
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
在我的项目中,我正在尝试获取数据,数据在数组中,所以我执行了所需的步骤,但数组在数据中有一个我不知道如何传递的名称"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 of type org.json.JSONObject cannot be converted to 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" 传递给我的响应以便访问数据?
您必须使用 JsonObjectRequest
而不是 JsonArrayRequest
因为您的回复实际上是 JSONObject
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("listCar");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
CarClass car = new CarClass();
car.setTitle(jsonObject.getString("adTitle"));
car.setDate(jsonObject.getString("adDate"));
// Left of the implementation
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);