1
我無法將JSON響應映射到自定義類的數組列表。Jackson POJO映射ArrayList <Class>無法識別的字段
問題是它無法識別JSON響應中的「userswithdistance」字段,即使我在JSONResponse類中將其設置爲ArrayList。錯誤:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "userswithdistance" (class com.az.d.classes.models.JSONResponse), not marked as ignorable (4 known properties: "success", "usersWithDistance", "action", "error"])
這裏是JSON響應
{
"action": "get_users",
"success": 1,
"error": 0,
"userswithdistance": [
{
"usr_id": "4",
"distance": 9896.348
},
{
"usr_id": "5",
"distance": 11536.063
}
]
}
這是JSONResponse類來包裝JSON響應。
public class JSONResponse {
private String _action;
private int _success;
private int _error;
private ArrayList<UsersWithDistance> _userswithdistance;
public String getAction() { return _action; }
public int getSuccess() { return _success; }
public int getError() { return _error; }
public ArrayList<UsersWithDistance> getUsersWithDistance() { return _userswithdistance; }
public void setAction(String value) { _action = value; }
public void setSuccess(int value) { _success = value; }
public void setError(int value) { _error = value; }
public void setUsersWithDistance(ArrayList<UsersWithDistance> value) { _userswithdistance = value; }
}
這裏是UsersWithDistance類。
public class UsersWithDistance {
private String _usr_id;
private double _distance;
public String getUsr_id() { return _usr_id; }
public double getDistance() { return _distance; }
public void setUsr_id(String value) { _usr_id = value; }
public void setDistance(double value) { _distance = value; }
}
我在Java中使用的代碼是:
JSONResponse result = mapper.readValue(URL, JSONResponse.class);
你救了我。謝謝! – Boy