Sample JSON data from Facebook
{
"1111111" : {
"home" : false,
"activities" : "some value"
},
"2222222" : {
"home" : false,
"activities" : "some value again"
}
}
public class Profile{
private boolean home;
private String activities;
// generated setter getter
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
Profile mainProfile = mapper.readValue(new File("data.json"), Profile.class);
System.out.println(mainProfile.getActivities().size());
}
}
Run above file will produce this error.
Unrecognized field "1111111" (Class com.analysis.structure.Profile), not marked as ignorable
The problem I’m facing is that how to map that “1111111” value to a variable in a class? If I use @JsonIgnoreProperties(ignoreUnknown=true), it will totally ignore all the josn data, since the first data does not have any tag to map to. How should I map this type of json data into Java using Jackson JSON?
That does not map to POJOs cleanly, so maybe you should rather bind it to a
Map, where key is of typeString, and value some POJO type?