I am using Gson to convert JSON into a Java object. I have a field in the json that our wise services people coded as either an array or object depending on how many items come back in database. Question is how do model the Java object to pass into Gson converter so that I can handle both types?
json = new Gson().fromJson(reader, Order.class);
Java Classes only parses the JSON array properly
public class Order {
private Detail detail
}
public class Detail {
public String id;
public List<Type> types;
//// getters and setters
}
public class Type {
public String typeId;
public String typeName
//// getters and setters
}
JSON data array
{
"detail":{
"id":"1234565",
"types":{
"type":[
{"typeId":"1246565","typeName":"MyName1"},
{"typeId":"1444445","typeName":"MyName2"}
]
}
}
}
JSON data object
{
"detail":{
"id":"1234565",
"types":{
"type":{"typeId":"1246565","typeName":"MyName1"}
}
}
}