2011-03-27 234 views
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. 
+0

這仍然是個問題嗎?如果是這樣,dealForPortfolioString的價值是什麼?它看起來不是代碼所期望的結構的JSON。 – 2011-06-10 16:44:43

回答

0

GSON預期的JSON對象的列表,如:[{obj1}, {obj2}, ... , {objN}]

我不是你張貼在這裏的代碼肯定你的意圖是什麼,但在我看來,像你用父對象包裝一個列表。

在這種情況下,你需要製作自己的deserializer