I want to deserialize an array of complex objects
for instance i have object MyTestObject
public class MyTestObject{
String param1;
String param2;
int param3;
}
now i have an array of objects named MyTestObject
and my json looks like this
{
[
"param1":"something1",
"param2:"something2",
"param3":3
],
[
"param1":"something1",
"param2:"something2",
"param3":3
],
[
"param1":"something1",
"param2:"something2",
"param3":3
]
}
this is my code for desearilization
public static <T> List<T> fromJSON(String json, Class<T> className) {
GsonBuilder builder = new GsonBuilder();
builder.serializeNulls();
Type myType = new TypeToken<List<T>>() {
}.getType();
Gson g1 = builder.create();
List<T> objects = (List<T>) g1.fromJson(json, (Type) myType);
for (T object : objects) {
Log.d(TAG, "object: " + object + " class " + object.getClass());
}
return objects;
}
ArrayList<MyTestObject> objects = fromJSON(myjson,MyTestObject.class);
the array itself gets deserialized correctly, however, the objects inside it are not of the correct class, they are of type com.google.gson.internal.LinkedTreeMap
when i need them to be of type MyTestObject
how do i fix this?