Im converting a json to a java object but I'm not getting what I want. I have duplicated keys "friend" in my json.
My json:
{
"id" : "5ee2e2f780bc8e7511a65de9",
"friends": [{
"friend": {
"id": 1,
"name": "Priscilla Lynch"
},
"friend": {
"id": 2,
"name": "William Lawrence"
}
}]
}
Using readValue from ObjectMapper only takes the last one "friend" but I need both. I know JSONObject use Map to convert so that's why it is taking the last one.
Result: Contacts(id=5ee2e2f780bc8e7511a65de9, friends=[{friend={id=2, name=William Lawrence}}])
ObjectMapper mapper = new ObjectMapper();
Contacts contacts = mapper.readValue(json, Contacts.class);
Contacts Pojo:
@Getter
@Setter
@ToString
public class Contacts {
String id;
List<Object> friends;
}
I want a list of all friends. As the service whose provide the json is not in my hand I need to find a way to solve it. I tried use MultiMap from apache.commons but no success. Im stuck on this.