我想通过谷歌Gson传输一个列表对象,但我不知道如何反序列化泛型类型。
看完这篇文章(BalusC的回答)后,我尝试的是:
MyClass mc = new Gson().fromJson(result, new List<MyClass>() {}.getClass());
但是然后我在Eclipse中得到一个错误,说“类型新列表<MyClass>(){}必须实现继承的抽象方法…”,如果我使用一个快速修复,我得到了一个超过20个方法存根的怪物。
我很确定有一个更简单的解决办法,但我似乎找不到!
现在我有了这个:
Type listType = new TypeToken<List<MyClass>>() {}.getType();
MyClass mc = new Gson().fromJson(result, listType);
然而,我确实在fromJson行得到以下异常:
java.lang.NullPointerException
at org.apache.harmony.luni.lang.reflect.ListOfTypes.length(ListOfTypes.java:47)
at org.apache.harmony.luni.lang.reflect.ImplForType.toString(ImplForType.java:83)
at java.lang.StringBuilder.append(StringBuilder.java:203)
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:56)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:64)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
at com.google.gson.Gson.fromJson(Gson.java:568)
at com.google.gson.Gson.fromJson(Gson.java:515)
at com.google.gson.Gson.fromJson(Gson.java:484)
at com.google.gson.Gson.fromJson(Gson.java:434)
我做捕获JsonParseExceptions和结果不是空。
我用调试器检查了listType,得到了以下结果:
列表类型
args = ListOfTypes
List = null
resolvedTypes =类型[1]
loader = PathClassLoader
ownerType0 = null
ownerTypeRes = null
rawType =类(java.util.ArrayList)
rawTypeName = "java.util.ArrayList"
因此,getClass调用似乎没有正常工作。有什么建议吗?
我查过Gson用户指南。它提到了在将泛型类型解析为Json时应该发生的运行时异常。我做得“错误”(上面没有显示),就像在示例中一样,但根本没有得到异常。所以我按照用户指南的建议改变了序列化。不过,这也无济于事。
编辑:
解决了,请看下面我的答案。
关于Gson的“Type”类理解,请参考示例2。
例1:在这个deserilizeResturant中,我们使用Employee[]数组并获取详细信息
public static void deserializeResturant(){
String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
Gson gson = new Gson();
Employee[] emp = gson.fromJson(empList, Employee[].class);
int numberOfElementInJson = emp.length();
System.out.println("Total JSON Elements" + numberOfElementInJson);
for(Employee e: emp){
System.out.println(e.getName());
System.out.println(e.getEmpId());
}
}
示例2:
//Above deserilizeResturant used Employee[] array but what if we need to use List<Employee>
public static void deserializeResturantUsingList(){
String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
Gson gson = new Gson();
// Additionally we need to se the Type then only it accepts List<Employee> which we sent here empTypeList
Type empTypeList = new TypeToken<ArrayList<Employee>>(){}.getType();
List<Employee> emp = gson.fromJson(empList, empTypeList);
int numberOfElementInJson = emp.size();
System.out.println("Total JSON Elements" + numberOfElementInJson);
for(Employee e: emp){
System.out.println(e.getName());
System.out.println(e.getEmpId());
}
}
关于Gson的“Type”类理解,请参考示例2。
例1:在这个deserilizeResturant中,我们使用Employee[]数组并获取详细信息
public static void deserializeResturant(){
String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
Gson gson = new Gson();
Employee[] emp = gson.fromJson(empList, Employee[].class);
int numberOfElementInJson = emp.length();
System.out.println("Total JSON Elements" + numberOfElementInJson);
for(Employee e: emp){
System.out.println(e.getName());
System.out.println(e.getEmpId());
}
}
示例2:
//Above deserilizeResturant used Employee[] array but what if we need to use List<Employee>
public static void deserializeResturantUsingList(){
String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
Gson gson = new Gson();
// Additionally we need to se the Type then only it accepts List<Employee> which we sent here empTypeList
Type empTypeList = new TypeToken<ArrayList<Employee>>(){}.getType();
List<Employee> emp = gson.fromJson(empList, empTypeList);
int numberOfElementInJson = emp.size();
System.out.println("Total JSON Elements" + numberOfElementInJson);
for(Employee e: emp){
System.out.println(e.getName());
System.out.println(e.getEmpId());
}
}