I am trying to create Dto class in Java for a json which is similar to this.
{
"abcList":[
{
"keyA":"valA",
"lstB":[{
"keyC":"valC"
},{
"keyC":"valC1"
}]
},{
"keyA":"valA",
"lstB":{
"keyC":"valC2"
}
}
]
}
Is the Json even valid? Inside the list abcList, the first object has a list "lstB" and in the second object, the field "lstB" is not a list.
If the json is valid, how would I create the corresponding Java Classes? This does not work.
class TopObject{
List<ABC> abcList;
}
class ABC{
public String keyA;
public List<B> lstB;
}
class B{
public String keyC;
}
I mean I can parse it successfully with gson, iterating through every element of the json with a code like below,
if(lstB.isJsonArray()){
System.out.println("lstb is array");
List<B> arLstB = parseBArray(lstB);
abc.setLstB(arLstB);
}else{
System.out.println("lstb is not array");
B b = parseB(lstB);
abc.addB(b);
}
but the original json is very large and i will end up writing the iterating logic for days. I want a solution where I can annotate it properly and do the mapping in one shot like this.
gson.fromJson(jsonInString, TopObject.class);