I'm having trouble mapping a JSON response with an arraylist of custom class.
The problem is it cannot recognize "userswithdistance" field in JSON response even I made it an ArrayList in JSONResponse class. 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
{
"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.
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.
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:
JSONResponse result = mapper.readValue(URL, JSONResponse.class);
You have two options:
I. Change setter method name to:
public void setUserswithdistance(ArrayList<UsersWithDistance> _userswithdistance) {
this._userswithdistance = _userswithdistance;
}
II. Add JsonProperty annotation:
@JsonProperty("userswithdistance")
private ArrayList<UsersWithDistance> _userswithdistance;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With