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的更多相关文章

  1. 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方法时,如果它的属性里面包含 ...

  2. json 字符串包含数组转换为object对象是报异常java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to

    前台传到后台的json字符串 前台实现这种格式json字符串方式: function contentFun(){ respType = respTypeFun(); return "{\&q ...

  3. 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, 查询出来的 ...

  4. 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 这种错误非常常见, ...

  5. JSON字符串转JavaBean,net.sf.ezmorph.bean.MorphDynaBean cannot be cast to ……

    在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名.年龄等 public class Student implements java.io.Serializab ...

  6. net.sf.ezmorph.bean.MorphDynaBean cannot be cast to java.util.Map

    public static void main(String[] arg0) { String reString = "{" + "\"code\": ...

  7. 【明哥报错簿】之json转换报错---net.sf.ezmorph.bean.MorphDynaBean cannot be cast to XXXDO

    简单的json和bean转换直接用: public static void main(String[] args) { String s = "{'request': [{'orderCod ...

  8. Caused by: java.lang.ClassNotFoundException: net.sf.ezmorph.Morpher

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher a ...

  9. Java中使用json时java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher问题解决

    下面代码: public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("msg& ...

随机推荐

  1. Java中使用Timer和TimerTask实现多线程

    转自:http://www.bdqn.cn/news/201305/9303.shtml 摘要:Timer是一种线程设施,用于安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行,可以 ...

  2. Simple PWM to Analog Circuit (0-10vdc)

    i just finished this simple circuit and am very satisfied with the result. The output is very stable ...

  3. WCF:又是枚举惹的祸

    在WCF中使用枚举不便于服务的演化,因为增加一个枚举值,需要更新所有客户端.某种程度上说这也带来了好处,即:防止了新增枚举值带来的意外(宁可失败,也不意外). 鉴于枚举的这种表现,以后尽可能的采用in ...

  4. Unity3D游戏制作(三)——移动平台上的角色阴影制作

    本系列文章由 Amazonzx 编写,欢迎转载,转载请注明出处. http://blog.csdn.net/amazonzx/article/details/7973740 本文将重点介绍两种目前在移 ...

  5. mysql从库Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'报错处理

    年后回来查看mysql运行状况与备份情况,登录mysql从库查看主从同步状态 mysql> show slave status\G; *************************** . ...

  6. idea html,js修改不用重启进程

    1Setting -> build-compiler ---勾选  Build project automatically选项 2 快捷键Ctrl + Shift + A查找registry命令 ...

  7. 关于JAVA 中的Configuration类

    properties文件是Java平台默认的配置文件格式,其优点是格式清晰,简单易懂,使用commons-configuration读取properties文件也比较简单,代码如下: 基本用法: 1. ...

  8. Emoji 编码

    https://segmentfault.com/a/1190000007594620 http://cenalulu.github.io/linux/character-encoding/ http ...

  9. node库的选择

    mongodb mongodb:524335 mongodb官方库 mongoose:252190 mongodb封装库 mongodb封装较少 websocket socket.io:1,148,2 ...

  10. SpringCloud服务间调用

    SpringCloud服务间的调用有两种方式:RestTemplate和FeignClient.不管是什么方式,他都是通过REST接口调用服务的http接口,参数和结果默认都是通过jackson序列化 ...