谷歌Gson工具类

来源:互联网 发布:在线抓阄软件 编辑:程序博客网 时间:2024/06/01 09:15
对于大部分公司使用JsonArray这样对Json数据处理的方法,我更推崇谷歌的Gson工具类.
主要也是自己用到,而且比较方便.


对于任何类型都能转换.

Gson gson = new Gson();
SingleRoleParam srp = new SingleRoleParam();
gson.toJson(srp);//转换到gson对象

ResultMessage result  = gson.fromJson(str, ResultMessage.class);//转换成对应类型,反射形式


对于List类型
ResultMessage result = gson.fromJson(str, ResultMessage.class);
Type listType = new TypeToken<LinkedList<FindRoleResult>>() {}.getType();
LinkedList<FindRoleResult> result2 = gson.fromJson(result.getMsg(),
listType);
Iterator<FindRoleResult> iterator = result2.iterator();
while (iterator.hasNext()) {
FindRoleResult user = (FindRoleResult) iterator.next();

进行操作即可.
0 0
原创粉丝点击