问题描述
想试试jsonObject组成JSON数据,可是一直报错,这是为啥呢public static void main(String[] args) throws Exception { Map map = new HashMap();map.put("name", "json");map.put("bool", "Boolean.TRUE");map.put("int", "new Integer(1)");map.put("func", "function(i){ return this.arr[i]; }");System.out.println(map);JSONObject json = JSONObject.fromObject(map);System.out.println(json); } {int=new Integer(1), name=json, func=function(i){ return this.arr[i]; }, bool=Boolean.TRUE}Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeExceptionat java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(ClassLoader.java:621)at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)at java.net.URLClassLoader.access$000(URLClassLoader.java:56)at java.net.URLClassLoader$1.run(URLClassLoader.java:195)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(URLClassLoader.java:188)at java.lang.ClassLoader.loadClass(ClassLoader.java:307)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)at java.lang.ClassLoader.loadClass(ClassLoader.java:252)at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)at com.test.JsonSample.main(JsonSample.java:21)Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeExceptionat java.net.URLClassLoader$1.run(URLClassLoader.java:200)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(URLClassLoader.java:188)at java.lang.ClassLoader.loadClass(ClassLoader.java:307)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)at java.lang.ClassLoader.loadClass(ClassLoader.java:252)at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)... 13 more
解决方案
缺少jar包,使用Object不只依赖当前的存有JsonObject对象的包,还有些其他Jar包。要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: 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