基本 JSON-lib 示例
我正在尝试使用 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,但也遇到了类似的问题。
我需要一些具有自由许可证的东西,比如 Apache 2、MIT 或类似的。
I'm trying to use JSON-lib, but I can't get it to run without NoClassDefFoundError
. Here's the code:
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());
}
}
And the command to compile:
javac -cp lib/json-lib-2.4-jdk15.jar hello.java
And the command to run:
java -cp .:lib/json-lib-2.4-jdk15.jar:lib/commons-lang-2.4.jar hello
I have also tried it with commons-lang3.3, which gives me different errors. I think it might be a version thing.
How do I compile and run a simple example with this library?
If there's a better library without crazy dependencies, I would love to hear about it. I've tried Douglas Crockford's JSON-Java, but I had similar problems.
I would need something with a liberal license, like Apache 2, MIT or similar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您寻求的答案就在 POM 文件 https://repository.sonatype.org/service/local/repositories/central-proxy/content/net/sf/json-lib/json-lib/2.4/json-lib-2.4.pom< /a>
您需要以下依赖项:
可选
该项目的网站 (http://json-lib.sourceforge.net/) 也列出了这些要求。
commons-lang-2.6 很可能会与 json-lib 2.4 一起使用,但我不能保证 commons-lang-3.x 也能使用同样的功能。
The answer you seek is right there in the POM file https://repository.sonatype.org/service/local/repositories/central-proxy/content/net/sf/json-lib/json-lib/2.4/json-lib-2.4.pom
You need the following dependencies:
optional
The project's website (http://json-lib.sourceforge.net/) also lists these requirements.
It's very likely that commons-lang-2.6 will work with json-lib 2.4 however I cannot guarantee the same for commons-lang-3.x.
请查看 google Gson:
这是示例:
瞧!
Take a look at google Gson instead:
This is example:
And voila!