我正在尝试使用JSON-lib,但是没有NoClassDefFoundError我无法让它运行。代码如下:
import net.sf.json.*;
public class hello {
public static void main(String[] args) {
String settings = "{\"hello\": \"world\"}";
JSONObject obj = (JSONObject)JSONSerializer.toJSON(settings);
System.out.println(obj.toString());
}
}以及要编译的命令:
javac -cp lib/json-lib-2.4-jdk15.jar hello.java以及要运行的命令:
java -cp .:lib/json-lib-2.4-jdk15.jar:lib/commons-lang-2.4.jar hello我也尝试过commons-lang3.3,它给了我不同的错误。我认为这可能是一个版本的事情。
如何使用这个库编译和运行一个简单的示例?
如果有一个没有疯狂依赖的更好的库,我很想听一听。我尝试过Douglas Crockford的JSON-Java,但我遇到了类似的问题。
我需要一些有自由许可的东西,比如Apache2,MIT或类似的。
发布于 2012-01-26 23:43:18
您要寻找的答案就在POM文件https://repository.sonatype.org/service/local/repositories/central-proxy/content/net/sf/json-lib/json-lib/2.4/json-lib-2.4.pom中
您需要以下依赖项:
commons-beanutils-1.8.0
commons-collections-3.2.1
commons-lang.2.5
commons-logging-1.1.1
ezmorph-1.0.6可选
xom.1.1 (if serializing from/to XML)
oro-2.0.8 (if using the jdk13 version of the library)该项目的网站(http://json-lib.sourceforge.net/)也列出了这些要求。
commons-lang-2.6很可能会与json-lib 2.4一起工作,但是我不能保证commons-lang-3.x也是如此。
发布于 2012-01-26 10:21:48
取而代之的是google Gson:
No other dependancies
的
下面是示例:
import com.google.gson.Gson;
class Foo {
private String hello;
public String toString() {
return "hello='" + hello + "'";
}
}
public class hello {
public static void main(String[] args) {
String text = "{\"hello\": \"world\"}";
Gson gson = new Gson();
Foo foo = gson.fromJson(text, Foo.class);
System.out.println(foo.toString());
System.out.println(gson.toJson(foo));
}
}瞧!
$ javac -cp lib/gson-2.0.jar hello.java
$ java -cp .:lib/gson-2.0.jar hello
hello='world'
{"hello":"world"}
$https://stackoverflow.com/questions/9012990
复制相似问题