java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to
Java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to
在使用JSONObject.toBean的时候,得到的Bean里面的复杂数据类型不能转换成需要的对象类型。
demo:
创建两个实体类Teacher和Student:Teacher中有一个List类型的私有属性
package com.edu.xukai; /**
* @author xukai
*
*/
public class Student { private String stuNo; private String stuName; public Student() {
} public Student(String stuNo, String stuName) {
this.stuNo = stuNo;
this.stuName = stuName;
} // getter setter @Override
public String toString() {
return "Student [stuNo=" + stuNo + ", stuName=" + stuName + "]";
} }
package com.edu.xukai; import java.util.List; /**
* @author xukai
*
*/
public class Teacher { private String teaId; private String teaName; private List<Student> stus; public Teacher() {
} public Teacher(String teaId, String teaName, List<Student> stus) {
this.teaId = teaId;
this.teaName = teaName;
this.stus = stus;
}
//getter setter }
测试之前,需要导入相应的jar文件。
需要使用JSONObject,使用的jar文件是json-lib-2.2.3-jdk15.jar
下面是可能缺少jar文件导致的错误和对应的jar:
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
commons-lang-2.4.jar
Caused by: java.lang.ClassNotFoundException: net.sf.ezmorph.Morpher
ezmorph-1.0.6.jar
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
commons-logging-1.1.1.jar
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.map.ListOrderedMap
commons-collections-3.2.1.jar
Caused by: java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean
commons-beanutils-1.7.0.jar
jar下载链接:点击下载
测试类:
package com.edu.xukai; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import net.sf.json.JSONObject; /**
* @author xukai
*
*/
public class TestJSONObject { public static void main(String[] args) {
Student student_1 = new Student("学号1", "学生1");
List<Student> stus = new ArrayList<Student>();
stus.add(student_1);
Teacher teacher_1 = new Teacher("编号1", "教师1", stus);
JSONObject obj = JSONObject.fromObject(teacher_1);
System.out.println("JSON格式的Teacher->" + obj.toString());
Teacher teacherBean = (Teacher) JSONObject.toBean(obj, Teacher.class);
try {
Student studentBean = teacherBean.getStus().get(0);
System.out.println(studentBean);
} catch (Exception e) {
System.out.println("出现异常");
e.printStackTrace();
}
} }
运行可以看到控制台打印结果:
JSON格式的Teacher->{"stus":[{"stuName":"学生1","stuNo":"学号1"}],"teaId":"编号1","teaName":"教师1"}
出现异常
java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.edu.xukai.Student
at com.edu.xukai.TestJSONOb ject.main(TestJSONObject.java:25)
解决办法:使用JSONObject.toBean(jsonObject, beanClass, classMap)
package com.edu.xukai; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import net.sf.json.JSONObject; /**
* @author xukai
*
*/
public class TestJSONObject { public static void main(String[] args) {
Student student_1 = new Student("学号1", "学生1");
List<Student> stus = new ArrayList<Student>();
stus.add(student_1);
Teacher teacher_1 = new Teacher("编号1", "教师1", stus);
JSONObject obj = JSONObject.fromObject(teacher_1);
System.out.println("JSON格式的Teacher->" + obj.toString()); // 定义一个Map
Map<String, Class<Student>> map = new HashMap<String, Class<Student>>();
map.put("stus", Student.class); // key为teacher私有变量的属性名
// 使用JSONObject.toBean(jsonObject, beanClass, classMap)
Teacher teacherBean = (Teacher) JSONObject.toBean(obj, Teacher.class, map);
try {
Student studentBean = teacherBean.getStus().get(0);
System.out.println(studentBean);
} catch (Exception e) {
System.out.println("出现异常");
e.printStackTrace();
}
} }
控制台打印结果:
JSON格式的Teacher->{"stus":[{"stuName":"学生1","stuNo":"学号1"}],"teaId":"编号1","teaName":"教师1"}
Student [stuNo=学号1, stuName=学生1]
java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to的更多相关文章
- json解析出现:java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to XXX
感谢大佬:https://blog.csdn.net/one_ink/article/details/99817676 一.出错原因 当我们利用json解析中的toBean方法时,如果它的属性里面包含 ...
- json 字符串包含数组转换为object对象是报异常java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to
前台传到后台的json字符串 前台实现这种格式json字符串方式: function contentFun(){ respType = respTypeFun(); return "{\&q ...
- java.util.LinkedHashMap cannot be cast to xxx 和 net.sf.ezmorph.bean.MorphDynaBean cannot be cast to xxx
java.util.LinkedHashMap cannot be cast to com.entity.Person 使用mybatis, resultMap映射的是实体类Person, 查询出来的 ...
- json学习系列(5)-json错误解析net.sf.ezmorph.bean.MorphDynaBean cannot be cast to
最近在使用json的时候,报了下面的错误: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to java.lang.String 这种错误非常常见, ...
- JSON字符串转JavaBean,net.sf.ezmorph.bean.MorphDynaBean cannot be cast to ……
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名.年龄等 public class Student implements java.io.Serializab ...
- net.sf.ezmorph.bean.MorphDynaBean cannot be cast to java.util.Map
public static void main(String[] arg0) { String reString = "{" + "\"code\": ...
- 【明哥报错簿】之json转换报错---net.sf.ezmorph.bean.MorphDynaBean cannot be cast to XXXDO
简单的json和bean转换直接用: public static void main(String[] args) { String s = "{'request': [{'orderCod ...
- Caused by: java.lang.ClassNotFoundException: net.sf.ezmorph.Morpher
1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher a ...
- Java中使用json时java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher问题解决
下面代码: public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("msg& ...
随机推荐
- NullPointerExceptio异常出现的情况
NullPointerException是java.lang.NullPointerException的简称,是Java语言中的一个异常类,位于java.lang包中,父类是java.lang.Run ...
- Redis淘汰删除策略
Redis淘汰删除策略 Redis淘汰删除策略6种淘汰Key策略3种删除过期键策略定时删除惰性删除定期删除其他模块的淘汰处理RDB 快照持久化创建载入AOF 只追加持久化写入重写主从复模式下对过期键的 ...
- C#的?和??
1.?? 为了实现Nullable数据类型转换成non-Nullable类型数据,才有的一个操作符: 意义:一变量取值,取符号左边的值,若左边为null,那么取赋值??右边的: 栗子:int a=3 ...
- mongodb group操作 以及管道 aggregate 分组排序分页
分组获取数据: db.express_info.group({ "key":{"express_code":true}, "initial" ...
- GOCN每日新闻(2017-08-12)
1.为什么我们从 Python 转向 Go 2.Go 实现常见数据结构 3.3年半,50万行 Go 代码 4.Go 游戏引擎 oka 5.Go 是隐藏学习曲线的语言
- Makefile 中的.PHONY
PHONY 目标并非实际的文件名:只是在显式请求时执行命令的名字.有两种理由需要使用PHONY 目标:避免和同名文件冲突,改善性能. 所谓的PHONY这个单词就是伪造的意思,makefile中将.PH ...
- javax.crypto.BadPaddingException: Given final block not properly padded解决方案
解密的时候报错: javax.crypto.BadPaddingException: Given final block not properly padded 该异常是在解密 ...
- 如何在IntelliJ IDEA中使用Git .ignore插件忽略不必要提交的文件
参考 https://blog.csdn.net/qq_34590097/article/details/56284935 最近初学Git,而且在使用的IDE是IntelliJ IDEA,发现IDE ...
- 流媒体Red5服务自定义媒体文件路径
4. 创建类实现自定义媒体访问路径 使用bean对自己的项目进行配置,更换默认的视频播放目录和视频录制目录.在0.6版的时候, 我们可以直接在red5-web.properties中写入playbac ...
- MAC常用软件推荐
SQL建模 http://dbwrench.com/download/install/mac_install.shtml 参考https://github.com/helantao/macOS/blo ...