I have a question which is similar to some questions at stackoverflow but none really answer my problem. I use the ObjectMapper
of Jackson and want to parse this JSON string into an List of User objects:
[{ "user" : "Tom", "role" : "READER" },
{ "user" : "Agnes", "role" : "MEMBER" }]
I define an inner class like this:
public class UserRole {
private String user
private String role;
public void setUser(String user) {
this.user = user;
}
public void setRole(String role) {
this.role = role;
}
public String getUser() {
return user;
}
public String getRole() {
return role;
}
}
To parse the JSON String to an List of UserRoles
I use generics:
protected <T> List<T> mapJsonToObjectList(String json) throws Exception {
List<T> list;
try {
list = mapper.readValue(json, new TypeReference<List<T>>() {});
} catch (Exception e) {
throw new Exception("was not able to parse json");
}
return list;
}
But what I get back is a List
of LinkedHashMaps
.
What is wrong with my code?