我在我的Android应用程序中使用volley和gson libs,主要目的是调用服务器API并检索一些数据。
一切运行良好,期望嵌套的JSON对象数据。我无法检索位于对象内部的信息(lat,lng)。
它抛出一个错误:
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;
}
你有什么建议?
答案 0 :(得分:0)
在您的JSON中,position
是一个对象,而不是一个数组。因此,在您的班级Object
(奇怪的名称)中,请private List<PositionObj> position;
更改private PositionObj position;
。