net.sf.ezmorph.bean.MorphDynaBean cannotbe cast to xxx
在操作json的数据格式的时候,如果没有指明数据类型,那么只能是基本类型或者是String类型,不能出现复杂数据类型。
for(Map<String, Object> map : datas){
String gw_id = map.get("GW_ID").toString();
gw_id = gw_id.substring(0, 14)+"1"+gw_id.substring(15);
map.put("GW_ID", gw_id);
map.put("UPDATE_TIME", new Date().getTime());
map.put("CREATE_TIME", map.get("CREATE_TIME"));
map.put("LAST_COLLECT_TIME", map.get("LAST_COLLECT_TIME"));
Set<String> keys = map.keySet();
for(String key:keys){
if(null == map.get(key)){
map.put(key, "");
}
}
}
String jsondata = JSONArray.fromObject(datas).toString();
JSONArray jsonarray = JSONArray.fromObject(jsondata );
List<Map<String, Object>> objs = (List<Map<String, Object>>)JSONArray.toCollection(jsonarray, Map.class);
在这里,因为得到json字符串的时候用的是 JSONArray.fromObject(collenction),所有,在讲json字符串转换成json对象的时候,也只能用JSONArray.toCollection,并且,一定要带上后面的class参数。
将json字符串转换成对应的json对象以后,在遍历json对象时
for(Map<String, Object> data : objs)
data.get("LAST_COLLECT_NUM")
此时,get()方法得到的是对象,并且是只能转换成基本数据类型或者是String类型的,如果强制转化成复杂的类型,会提示这个错误。
再如:
Teacher类中,有三个属性。
private String teaId;
private String teaName;
private List<Student> stus;
Teacher teacher_1 = new Teacher("编号1", "教师1", stus);
JSONObject obj = JSONObject.fromObject(teacher_1);
这一步,将Teacher 对象转换成json字符串的时候,没有任何的问题。
下面,将JSONObject 转换成Teacher 对象,如果,不加后面的class参数,也会报这儿错误。加了后,这一步也正常。
Teacher teacherBean = (Teacher) JSONObject.toBean(obj, Teacher.class);
Student studentBean = teacherBean.getStus().get(0);
当从teacherBean 对象中取出stus属性的值时候,就会提示:
java.lang.ClassCastException:net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.edu.xukai.Student
用这种方式可以解决:
map.put("stus", Student.class);
Teacher teacherBean = (Teacher) JSONObject.toBean(obj, Teacher.class, map);
其中map对象是teacherBean对象中各个属性的类型,map额key是属性每次,value是属性的类型。
JSONArray.toArray(jsonArray, objectClass, classMap)
JSONArray.toList(jsonArray, objectClass, classMap)
也支持这样的方式。
分享到:
相关推荐
NULL 博文链接:https://hw1287789687.iteye.com/blog/1993048
以上是JSON所需的六个jar包,拿下来即可使用
JSON转化时出现 java.lang.ClassNotFoundException: net.sf.ezmorph.MorpherRegistry
Json所需6个JAR包(net.sf.ezmorph.MorpherRegistry异常)所需要的java 包
Json所需6个JAR包(net.sf.ezmorph.MorpherRegistry异常),在json使用中我们经常会遇到net.sf.ezmorph.MorpherRegistry异常,这些jar包就是为了解决这个问题
net.sf.json.JSONObject所需要的jar包 commons-beanutils-1.9.3.jar commons-collections-3.2.2 commons-lang-2.6 commons-logging-1.2 ezmorph-1.0.6 json-lib-2.4-jdk15
java开发中需要jsonObject,jsonArray需要用到6个jar包,net.sf.json.JSONObject commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging.jar ezmorph-1.0.3.jar json-lib-...
net.sf.json jar包包含了一下:可以用来解析json,具体操作可以看小编的博客。 commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar
net.sf.json使用的lib(包括依赖lib) 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar 5.ezmorph.jar 6.json-lib-2.2.2-jdk15.jar
net.sf.json依赖jar包,其中包括: commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging.jar ezmorph-1.0.3.jar json-lib-2.4-jdk15.jar
net.sf.json jar包含以下: commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar
JSONObject和JSONAarry的需要的jar,包括json-lib-2.4-jdk15,以及依赖commons-beanutils-1.9.3; commons-collections-3.2.2 ;commons-lang-2.6 ; ezmorph-1.0.6.jar
关于使用JSONArray.fromObject()方法和引入net.sf.json包所需要的依赖包:commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph.jar,morph-1.0.1.jar
commons-beanutils-1.9.3.jar、commons-collections-3.2.2.jar、commons-lang-2.6.jar、commons-logging-1.2.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar
json2.4相关依赖jar包json-lib-2.4-jdk15.jar/ezmorph-1.0.6.jar 等6个jar包
net.sf.json jar包包含了一下:可以用来解析json,具体操作可以看小编的博客。 commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar
NULL 博文链接:https://bijian1013.iteye.com/blog/2305804
JSONObject.jar 相关加包,包含 commons-beanutils-1.7.0.jar、commons-collections-3.1.jar、commons-lang-2.5.jar、commons-logging.jar、ezmorph-1.0.3.jar、json-lib-2.4-jdk15.jar等相关依赖。