关于Gson在强转时的ClassCastException
关于Gson的坑人指出:
将list转化为json
String beanListToJson = gson.toJson(list, type);
将json还原为list
List<T > objectList = gson.fromJson(resultTemp, type);
那么坑来了——List<T> list的这个T不能是Object(这里不是指语法错误)
Object有什么好处呢?
假如我有三个不一样的对象Student,Teacher,Master要放到List
list.add(student);
list.add(teacher);
list.add(master);
String beanListToJson = gson.toJson(list, type);
当你准备把json转成list的时候:
List<T > objectList = gson.fromJson(resultTemp, type);
Student student = (Student)objectList.get(0);
Teacher teacher = (Teacher)objectList.get(1);
Master master = (Master)objectList.get(2);
Duang!Duang!Duang!ClasscastException.无法强转。
那应该怎么做呢?
创一个对象People
Class People{
Private Student student;
Private Teacher teacher;
Priavate Master master
}
list.add(people);
String beanListToJson = gson.toJson(list, type);
List<People > objectList = gson.fromJson(resultTemp, type);
People people = objectList.get(0);
Student student = people.getStudent();
Teacher teacher = people.get Teacher ();
Master master = people.get Master ();
恭喜你,成功!
关于Gson在强转时的ClassCastException的更多相关文章
- 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题
通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署 ClassCastException异常 反射 ...
- 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题
1.问题描述 首先,需要解析的Json数据类似于下面的格式,但是包含了Java关键字abstract: { ret: 0, msg: "normal return.", news: ...
- 使用gson在解析unicode时遇到的问题
之前在用gson解析的时候未记录下来,所以今天做一个小的总结, 比如遇到像这种"\u003d"的unicode的字符,我们想解码这个字符,用gson可以这样表达 Gson gson ...
- [转]用GSON 五招之内搞定任何JSON数组
关于GSON的入门级使用,这里就不提了,如有需要可以看这篇博文 <Google Gson的使用方法,实现Json结构的相互转换> ,写的很好,通俗易懂. 我为什么写这篇文章呢?因为前几晚跟 ...
- Gson基本操作,JsonObject,JsonArray,String,JavaBean,List互转
(转自)https://www.cnblogs.com/robbinluobo/p/7217387.html String.JsonObject.JavaBean 互相转换 User user = n ...
- [转] Android:用GSON 五招之内搞定任何JSON数组
[From] http://www.open-open.com/lib/view/open1472632967912.html 写在前面 关于GSON的入门级使用,这里就不提了,如有需要可以看这篇博文 ...
- 引用变量的类型强转以及InstanceOf方法的使用
引用到的类: class Person{ String name; } class Student extends Person{ String sut_no; } class ClassMate e ...
- toString()、String.valueOf、(String)强转
1.基本类型 (1)基本类型没有toString()方法 (2)推荐使用String.valueOf(); (3)无法强转 =========补========= (String)是标准的类型转换,将 ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
随机推荐
- mac 安装mongodb与常用操作
1.安装 brew update brew install mongodb 2.启动mongo mongod --config /usr/local/etc/mongod.conf 3.启动 mong ...
- SVN文件上感叹号、加号、问号等图标的原因
黄色感叹号(有冲突): --这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别 ...
- mac pro上安装docker
1.进入一下地址进行下载docker https://download.docker.com/mac/stable/Docker.dmg 进入后进行下载后进行安装 2.将其拖动到Appliaction ...
- Python+Selenium 自动化实现实例-数据驱动实例
#coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.implicitly_wait(10) ...
- hive学习(六) 参数和动态分区
1.hive 参数.变量 1.1hive的命名空间: hive当中的参数.变量,都是以命名空间开头
- [转]Java中堆和栈创建对象的区别
转载自http://blog.csdn.net/hbhhww/article/details/8152838 栈与堆都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序 ...
- covariance matrix 和数据分布情况估计
how to get data covariance matrix: http://stattrek.com/matrix-algebra/covariance-matrix.aspx meaning ...
- Tornado入门资料整理
预备知识 没学过计网的苦逼找点现成一些的东西看吧…… <Restful Web Services>,<HTTP The Definitive Guide>,各种RFC WSGI ...
- 一个配置文件收集多个日志-if根据type类型判断
1.同时收集/var/log/messages日志和secure日志 #vim /etc/logstash/conf.d/system.conf input { file { path => & ...
- ubuntu 防火墙关闭的80端口,开启方法
#关闭防火墙 /etc/init.d/iptables stopservice iptables stop # 停止服务#查看防火墙信息/etc/init.d/iptables status #开放端 ...