我想反序列化一组复杂对象
例如我有对象MyTestObject
public class MyTestObject{
String param1;
String param2;
int param3;
}
现在我有一个名为的对象数组MyTestObject
我的json看起来像这样
{
[
"param1":"something1",
"param2:"something2",
"param3":3
],
[
"param1":"something1",
"param2:"something2",
"param3":3
],
[
"param1":"something1",
"param2:"something2",
"param3":3
]
}
这是我的去序列化代码
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);
数组本身被正确反序列化,但是,其中的对象不是正确的类,它们是类型com.google.gson.internal.LinkedTreeMap
当我需要它们是类型时MyTestObject
我该如何解决?