1
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);
}
}
现在我该怎么称呼它:解析JSON列出与通用领域的项目与GSON
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
没有,但我会得到一个答案很快 - 我要去测试它。谢谢 – DominikAngerer
你我的朋友真棒!完美的作品!现在读这个的每个人 - > Upvote它! – DominikAngerer