I want to convert the following JSON
string to a Java
object:
{
"user": {
"0": {
"firstName": "Monica",
"lastName": "Belluci"
},
"1": {
"firstName": "John",
"lastName": "Smith"
},
"2": {
"firstName": "Owen",
"lastName": "Hargreaves"
}
}
}
To convert this to Java
object I've created the following classes:
class User {
private Map<String, MyObject> user = new HashMap<>();
//Getter and Setter is here
}
class MyObject {
private String firstName;
private String lastName;
//Getters and Setters are here
}
I'm using Jackson library to convert JSON
to Java
. Here is how I'm using the Jackson for conversion:
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);
The problem is that with this conversion above the Map
inside the User object is always empty. What am I doing wrong?
Thanks in advance.