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

      

  • 相关阅读:
    MySQL复制的管理和维护
    error nr.1045 access denied for user 'root'@'localhost' (using passwd:no)
    MYSQL-使用mysqldump创建数据库快照
    ConcurrentBag同线程元素的添加和删除
    bootstrap源码学习与示例:bootstrap-tab
    Docker 私有仓库最简便的搭建方法
    使用Apache Jmeter进行并发压力测试
    cmd隐藏指定文件
    Bootstrap之BootstrapDialog
    全栈工程师的武器——MEAN(转)
  • 原文地址:https://www.cnblogs.com/a757956132/p/5609742.html
Copyright © 2020-2023  润新知