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.TestJSONObject.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]