使用json的时候出现NestableRuntimeException异常

来源:互联网 发布:现代房屋设计视频软件 编辑:程序博客网 时间:2024/05/18 01:49

如果出现下面的异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException

Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException


那么可能是版本出现问题,比如用了commons-lang-3.1的最新版本3.1的,就会报上面的错误。

使用json需要导入的包为:

commons-beanutils-1.8.3

commons-lang-2.6 

commons-collections-3.2.1

commons-logging-1.1.1

ezmorph-1.0.6


把json的字符串转成hashMap:

json字符串的格式大致为:{"name":"tom","age":"12"}

转化的语句:

private HashMap<String,Object> getHashMapFormJson(String jsonMsg){

JSONObject json = JSONObject.fromObject(jsonMsg);

HashMap<String,Object> result = json.toBean( json, new HashMap<String,Object>, new JsonConfig());

return result;

}


把json的字符串转成List:

json字符串的格式大致为:[{"name":"tom","age":"12"},{"name":"jack","age":"22"},........]

转化的语句:

private HashMap<String,Object> getHashMapFormJson(String jsonMsg){

JSONArray json = JSONArray.fromObject(jsonMsg);

List<HashMap<String,Object>> list = JSONArray.toList( json, new HashMap<String,Object>, new JsonConfig());

HashMap<String,Object> map1 = list.get(0);

HashMap<String,Object> map2 = list.get(1);

return map1;

}


转载的地址是:http://blog.csdn.net/wugewuge/article/details/8074272


原创粉丝点击