conversion between list of objects and Gson string

I'm converting a list of objects to json string and then back to list of objects as below: List list = new ArrayList(); Work w1 = new Work(); Work w2 = new Work(); list.add(w1); list.add(w2); String str = GsonUtils.getJsonString(list); Type listType = new TypeToken>() { }.getType(); List list1 = new Gson().fromJson(str, listType); this works completely fine and returns the list `List`. Now I'm doing same by extracting a method as below: List list = new ArrayList(); Work w1 = new Work(); Work w2 = new Work(); list.add(w1); list.add(w2); String str = GsonUtils.getJsonString(list); Type listType = new TypeToken>() { }.getType(); List list1 = getListFromJSON(str, Work.class); where method is defined as below: public List getListFromJSON(String str, Class type) { Type listType = new TypeToken>() { }.getType(); List list = new Gson().fromJson(str, listType); return list; } this time it's resulting into an error: Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.StringMap cannot be cast to com.restfb.types.User$Work at expt.ExptGsonList.main(ExptGsonList.java:45) Please help me to know where am I going wrong and how can I get this working using method?

以上就是conversion between list of objects and Gson string的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏