I have a JSON file with multiple entries that have same attribute names, but different attribute values, such as:
{
"name" : { "first" : "A", "last" : "B" },
"gender" : "MALE",
"married" : false,
"noOfChildren" : 2
},
{
"name" : { "first" : "C", "last" : "D" },
"gender" : "FEMALE",
"married" : true,
"noOfChildren" : 1
}
The class that it should be mapped is:
public class Human {
private Name name;
private String gender;
private int age;
<getter, setters etc>
}
EDIT: Service code is :
List<Human> humans = null;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
humans= objectMapper.readValue(json, new TypeReference<List<Human>>(){});
} catch (IOException e) {
e.printStackTrace();
}
JSON is parsed from HTTP entity and with correct format and now I added the annotation ass suggested in the answers.
As you can see, they have some attributes in common, but differ in others, and I would like to map those common fields. Is it possible to map the JSON this way ? I have tried mapping JSON to a collection/list/array of JsonNodes, but I keep getting erros about deserialization, while mapping only one instance of JSON entry works just fine. Is there another way of doing this ?