I am using GSON for parsing JSON String but there is key contains Json which is sometime object and sometimes array. So please help me for parse it to model class using gson.
Resonse with Array
{
"key" : "test",
"value" : [
{
"id" : 1,
"name": "abc"
},
{
"id" : 2,
"name": "xyz"
}
]
}
Resonse with Object
{
"key" : "test",
"value" : {
"id" : 1,
"name": "abc"
}
}
MyModel.java
public class MyModel implements Serializable {
@SerializedName("key")
@Expose
public String key;
@SerializedName("value")
@Expose
public ArrayList<ValueModel> value;
public class ValueModel implements Serializable {
@SerializedName("id")
@Expose
public String id;
@SerializedName("name")
@Expose
public String name;
}
}
But it always goto in exeption because of datatype array and object I also tried with JsonDeserializer but i think i didnt implement it well So please help me to resolve it and parse json