Gson反序列化集合 [英] Gson deserialize Collections

查看:66
本文介绍了Gson反序列化集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gson的新版本在Collections的反序列化方面有很多更改.在使用TypeToken以便从Json数组检索列表之前,但是TypeToken在库中不再存在.是否有任何示例说明现在采用哪种方法?……一直在查看文档,但目前尚不清楚.它建议实现JsonDeserializer接口,并使用GsonBuilder ...但是,尚不清楚.有没有人举过一个例子,说明我们如何使用Gson反序列化一个集合?非常感谢.

New versions of gson have a lot of changes regarding deserialization of Collections. Before we used TypeToken in order to retrieve a List from a Json Array, however TypeToken no longer exists in the library. Are there any examples of what approach to take now??... have been looking at the documentation but it's no clear about this. It suggested implementing the JsonDeserializer interface, and using GsonBuilder... buit again it's not clear. Does anybody has an example of how could we deserialize a collection by using Gson?. Thanks a lot.

推荐答案

google Gson的2.1版不再具有TypeToken类的公共构造函数,而应使用它提供的静态方法之一:

Version 2.1 of google Gson no longer has a public constructor for the TypeToken class, instead we should use one of the static methods provided by it:

IE:

List<MyClass> myList= new ArrayList<MyClass>();
Type listType = TypeToken.get(myList.getClass()).getType();
myList= (new Gson()).fromJson(jsonString, listType);

这篇关于Gson反序列化集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆