基本 JSON-lib 示例

发布于 2024-12-29 04:36:27 字数 773 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离笑几人歌 2025-01-05 04:36:27

您寻求的答案就在 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>

您需要以下依赖项:

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 也能使用同样的功能。

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:

commons-beanutils-1.8.0
commons-collections-3.2.1
commons-lang.2.5
commons-logging-1.1.1
ezmorph-1.0.6

optional

xom.1.1 (if serializing from/to XML)
oro-2.0.8 (if using the jdk13 version of the library)

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.

窗影残 2025-01-05 04:36:27

请查看 google Gson

  • Apache 许可证
  • 无其他依赖项
  • 简单用法

这是示例:

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"}
$

Take a look at google Gson instead:

  • Apache license
  • No other dependancies
  • Simple usage

This is example:

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));
    }
}

And voila!

$ javac -cp lib/gson-2.0.jar hello.java
$ java -cp .:lib/gson-2.0.jar hello
hello='world'
{"hello":"world"}
$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文