• 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]
    

      

  • 相关阅读:
    Ubuntu 永久修改DNS
    三大主流MQ的组织结构
    nginx基本配置
    CentOS7安装RabbitMQ
    单个表上亿行数据的主键、索引设计,及分页查询
    [SQL]行列转换
    《团队协作的五大障碍》读后感
    【2021-09-26】面对困难是未来绝不后悔的事情
    【2021-09-25】摇摆不定反逼自己脚踏实地
    【一句日历】2021年10月
  • 原文地址:https://www.cnblogs.com/a757956132/p/5609742.html
Copyright © 2020-2023  润新知