json-lib net.sf.ezmorph.bean.MorphDynaBean cannot be cast to xxx

在使用json-lib 反序列化时,报错:

Exception in thread "main" Java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.jn.json.bean.Student

at com.jn.test.Test2.test_reserialize(Test2.java:104)

at com.jn.test.Test2.main(Test2.java:110)

 

项目结构如下(源代码见附件):
json-lib net.sf.ezmorph.bean.MorphDynaBean cannot be cast to xxx
 依赖的库如下:
json-lib net.sf.ezmorph.bean.MorphDynaBean cannot be cast to xxx

json-lib-2.1-jdk15.jar


 测试代码如下:把字符串转化为java对象

Java代码  json-lib net.sf.ezmorph.bean.MorphDynaBean cannot be cast to xxx
  1. public static void test_reserialize(){  
  2. //      String jsonInput="{\"className\":\"计算机0705\",\"count\":0,\"students\":[{\"addrr\":null,\"age\":0,\"hobby\":\"\",\"name\":\"\"}]}";  
  3.         String jsonInput="{\"classAttribute\":{\"pp1\":\"vv1\",\"pp2\":\"vv2\"},\"className\":\"计算机0705\",\"count\":0,\"students\":[{\"addrr\":null,\"age\":0,\"attribute\":{\"p2\":\"v2\",\"p1\":\"v1\"},\"hobby\":\"\",\"name\":\"\"}]}";  
  4.         JSONObject js = JSONObject.fromObject(jsonInput);  
  5.         Class2 one = (Class2) JSONObject.toBean(js, Class2.class);  
  6.         System.out.println(one.getClassName());  
  7.         Map<String, Object> attribute =one.getStudents().get(0).getAttribute();  
  8.         System.out.println(attribute);  
  9.           
  10.     }  
  11.       
  12.     public static void main(String[] args) {  
  13.         test_reserialize();  
  14.     }  

 运行时报错:
json-lib net.sf.ezmorph.bean.MorphDynaBean cannot be cast to xxx
 

解决方法:

使用JsonConfig,修改后的代码如下:

Java代码  json-lib net.sf.ezmorph.bean.MorphDynaBean cannot be cast to xxx
  1. public static void test_reserialize(){  
  2.   
  3.         String jsonInput="{\"classAttribute\":{\"pp1\":\"vv1\",\"pp2\":\"vv2\"},\"className\":\"计算机0705\",\"count\":0,\"students\":[{\"addrr\":null,\"age\":0,\"attribute\":{\"p2\":\"v2\",\"p1\":\"v1\"},\"hobby\":\"\",\"name\":\"\"}]}";  
  4.         JSONObject js = JSONObject.fromObject(jsonInput);  
  5.           
  6.         JsonConfig jsonConfig = new JsonConfig();  
  7.   
  8.         jsonConfig.setRootClass(Class2.class);  
  9.         Map<String, Class> classMap = new HashMap<String, Class>();  
  10.         classMap.put("students", Student.class); // 指定JsonRpcRequest的request字段的内部类型  
  11.         jsonConfig.setClassMap(classMap);  
  12.           
  13.           
  14.           
  15.         Class2 one = (Class2) JSONObject.toBean(js, jsonConfig);  
  16.         System.out.println(one.getClassName());  
  17.         Map<String, Object> attribute =one.getStudents().get(0).getAttribute();  
  18.         System.out.println(attribute);  
  19.           
  20.     }  
  21.       

 主要增加了:

JsonConfig jsonConfig = new JsonConfig();

 

jsonConfig.setRootClass(Class2.class);

Map<String, Class> classMap = new HashMap<String, Class>();

classMap.put("students", Student.class); // 指定JsonRpcRequest的request字段的内部类型

jsonConfig.setClassMap(classMap);

上一篇:org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException


下一篇:ECS七天实践训练营入门班-Day5