You could try this for testing:
public static void main(String[] args) {
String jsonString = "{\n" +
" \"keyA\": {\n" +
" \"ID\": \"123\",\n" +
" \"Name\": \"TESTA\",\n" +
" \"Mobile\": \"1111\"\n" +
" },\n" +
" \"keyB\": {\n" +
" \"ID\": \"456\",\n" +
" \"Name\": \"TESTB\",\n" +
" \"Mobile\": \"2222\"\n" +
" }\n" +
"}";
List<CustomObject> customObjects = new ArrayList<CustomObject>();
ObjectMapper mapper = new ObjectMapper();
try {
ClassA myClassAObject= mapper.readValue(jsonString, ClassA.class);
customObjects.add(myClassAObject.getKeyA());
customObjects.add(myClassAObject.getKeyB());
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println(customObjects.size());
}
My other model classes, CustomObject
:
public class CustomObject {
private String id;
private String name;
private String mobile;
@JsonProperty("ID")
public String getId() {
return id;
}
@JsonProperty("ID")
public void setId(String id) {
this.id = id;
}
@JsonProperty("Name")
public String getName() {
return name;
}
@JsonProperty("Name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("Mobile")
public String getMobile() {
return mobile;
}
@JsonProperty("Mobile")
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
and ClassA
public class ClassA {
private CustomObject keyA;
private CustomObject keyB;
public CustomObject getKeyA() {
return keyA;
}
public void setKeyA(CustomObject keyA) {
this.keyA = keyA;
}
public CustomObject getKeyB() {
return keyB;
}
public void setKeyB(CustomObject keyB) {
this.keyB = keyB;
}
}
In case you have issues, this is the library version that I used in Maven
:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1.3</version>
</dependency>