【发布时间】:2014-07-01 08:06:49
【问题描述】:
public class OwnCollection<T>{
private int size;
private List<ResponseItem<T>> data;
}
public class ResponseItem<T>{
private String path;
private String key;
private T value;
}
public class Query{
public <T> OwnCollection<T> getParsedCollection( ... ){
String json = ...; //some unimportant calls where I get an valid Json to parse
return Result.<T>parseToGenericCollection(json);
}
}
public class Result{
public static <T> OwnCollection<T> parseToGenericCollection(String result){
Type type = new TypeToken<OwnCollection<T>>() {}.getType();
//GsonUtil is a class where I get an Instance Gson, nothing more.
return GsonUtil.getInstance().fromJson(result, type);
}
}
现在我怎么称呼它:
OwnCollection<Game> gc = new Query().<Game>getParsedCollection( ... );
结果我想,我会得到一个OwnCollection 和一个List<ResponseItem>,其中一个响应项包含Game 类的字段。 Json 非常好,没有解析错误,现在唯一的问题是当我尝试获取一个 Game 项目并调用方法时出现此错误:
Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to at.da.example.Game
【问题讨论】: