0
我試圖從數據庫反序列化一個數組,但沒有成功。
這是我把我的對象的方式:GSON - 反序列化java泛型集合
for (int i = 0; i < dealsList.size(); i++) {
ServerServicesInternalWrapper.reportDeal(Json.toWrap(dealsList.get(i)));
}
其中
public static <T> String toWrap(T t) {
JsonWrapperWithType wrapper =
new JsonWrapperWithType(t.getClass(),gson.toJson(t));
return to(wrapper);
}
這是我選擇
return (List<DealBean>)session.createCriteria(DealBean.class).add(Restrictions.eq("portfolio", portfolio)).list();
,然後我把它GSON使用
字符串的方法JsonWrapperWithType wrapper =
new JsonWrapperWithType(t.getClass(), gson.toJson(t));
return gson.toJson(wrapper);
,這是我的方式去GSON對象
而現在 - 對異常
Type listType = new TypeToken<List<DealBean>>(){}.getType();
List<DealBean> dealsForPortfolio =
(List<DealBean>)gson.fromJson(dealsForPortfolioString,type);
com.google.gson.JsonParseException: The JsonDeserializer
[email protected]9 failed to deserialized
json object {"type":"java.util.ArrayList","content":"[{\"ID\":5,\"tradable\":
...
Caused by: java.lang.IllegalStateException: This is not a JSON Array.
這仍然是個問題嗎?如果是這樣,dealForPortfolioString的價值是什麼?它看起來不是代碼所期望的結構的JSON。 – 2011-06-10 16:44:43