问题描述
我在Android应用程序中使用volley和gson库,其主要目的是调用服务器API并检索一些数据。
一切正常,需要嵌套的JSON对象数据。 我无法检索位置对象内部的信息(纬度,经度)。
它抛出一个错误:
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 99 path $.position
并指出了deliveryResponse方法。
我的JSON示例:
[{"number":18,"name":"John","address":"John Street.","position":{"lat":12.68300406,"lng":45.28001237},"status":"OPEN"},{"number":18,"name":"John","address":"John Street.","position":{"lat":12.68300406,"lng":45.28001237},"status":"OPEN",}]
deliveryResponse方法:
@Override
protected void deliverResponse(JSONArray jsonArray) {
if (mListener != null && jsonArray != null) {
List<T> responseArray = new ArrayList<T>();
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject entry = jsonArray.getJSONObject(i);
T parsedResponse = new Gson().fromJson(entry.toString(), mClass);
if (parsedResponse != null) {
responseArray.add(parsedResponse);
}
} catch (JSONException e) {
Log.d(TAG, "Error parsing JSON Object: " + e.getMessage());
mListener.onResponse(jsonArray);
}
}
mListener.onGsonResponse(responseArray);
}
}
对象类:
public class Object {
private int number;
private String name;
private String address;
private List<PositionObj> position;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<PositionObj> getPosition() {
return position;
}
public void setPosition(List<PositionObj> position) {
this.position = position;
}
PositionObj类:
public class PositionObj {
private int lat;
private int lng;
public int getLat() {
return lat;
}
public void setLat(int lat) {
this.lat = lat;
}
public int getLng() {
return lng;
}
public void setLng(int lng) {
this.lng = lng;
}
你建议什么?
1楼
在您的JSON中, position
是一个对象而不是数组。
因此,在您的类Object
(怪异名称)中,更改private List<PositionObj> position;
通过private PositionObj position;
。