[英]Jackson POJO Mapping ArrayList<Class> Unrecognized Field
I'm having trouble mapping a JSON response with an arraylist of custom class. 我在将JSON响应与自定义类的arraylist映射时遇到麻烦。
The problem is it cannot recognize "userswithdistance" field in JSON response even I made it an ArrayList in JSONResponse class. 问题是,即使我在JSONResponse类中将其设置为ArrayList,也无法识别JSON响应中的“ userswithdistance”字段。 Error was: 错误是:
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"])
Here is JSON response 这是JSON响应
{
"action": "get_users",
"success": 1,
"error": 0,
"userswithdistance": [
{
"usr_id": "4",
"distance": 9896.348
},
{
"usr_id": "5",
"distance": 11536.063
}
]
}
This is JSONResponse class to wrap the JSON response. 这是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; }
}
Here is UsersWithDistance class. 这是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; }
}
The code I used in Java was: 我在Java中使用的代码是:
JSONResponse result = mapper.readValue(URL, JSONResponse.class);
You have two options: 您有两种选择:
I. Change setter method name to: I.将setter方法名称更改为:
public void setUserswithdistance(ArrayList<UsersWithDistance> _userswithdistance) {
this._userswithdistance = _userswithdistance;
}
II. II。 Add JsonProperty
annotation: 添加JsonProperty
批注:
@JsonProperty("userswithdistance")
private ArrayList<UsersWithDistance> _userswithdistance;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.