Jackson POJO Mapping ArrayList <class>无法识别的字段</class>

时间:2013-11-26 21:23:07

标签: java android arrays jackson pojo

-->

我无法使用自定义类的arraylist映射JSON响应。

问题是它无法识别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
        }
    ]
}

这是用于包装JSON响应的JSONResponse类。

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);

1 个答案:

答案 0 :(得分:4)

你必须选择:

予。将setter方法名称更改为:

public void setUserswithdistance(ArrayList<UsersWithDistance> _userswithdistance) {
           this._userswithdistance = _userswithdistance;  
}

II。添加JsonProperty注释:

    @JsonProperty("userswithdistance")
    private ArrayList<UsersWithDistance> _userswithdistance;
相关问题
最新问题