Jackson POJO映射ArrayList <Class>无法识别的字段

Boy*_*Boy 2 java arrays android pojo jackson

我在将JSON响应与自定义类的arraylist映射时遇到麻烦。

问题是,即使我在JSONResponse类中将其设置为ArrayList,也无法识别JSON响应中的“ userswithdistance”字段。错误是:

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"])
Run Code Online (Sandbox Code Playgroud)

这是JSON响应

{
    "action": "get_users",
    "success": 1,
    "error": 0,
    "userswithdistance": [
        {
            "usr_id": "4",
            "distance": 9896.348
        },
        {
            "usr_id": "5",
            "distance": 11536.063
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

这是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; }
}
Run Code Online (Sandbox Code Playgroud)

这是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; }
}
Run Code Online (Sandbox Code Playgroud)

我在Java中使用的代码是:

JSONResponse result = mapper.readValue(URL, JSONResponse.class);
Run Code Online (Sandbox Code Playgroud)

Mic*_*ber 6

您有两种选择:

I.将setter方法名称更改为:

public void setUserswithdistance(ArrayList<UsersWithDistance> _userswithdistance) {
    this._userswithdistance = _userswithdistance;  
}
Run Code Online (Sandbox Code Playgroud)

二。添加JsonProperty注释:

@JsonProperty("userswithdistance")
private ArrayList<UsersWithDistance> _userswithdistance;
Run Code Online (Sandbox Code Playgroud)