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