这样的JSON字符串构建器是否存在?

时间:2022-09-25 23:25:24

I'm looking to do something like the following in Java and was wondering whether such a JSON library/helper already existed out there somewhere?

我希望在Java中执行类似下面的操作,并且想知道这样的JSON库/助手是否已存在于某处?

SomeJsonBuilder builder = new SomeJsonBuilder();
builder.add("one", "oneValue");
builder.add("two.three", "threeValue");
String output = builder.toString();

Such that the output string above would be something like:

这样上面的输出字符串将是这样的:

{"one":"oneValue", "two":{"three":"threeValue"}}

7 个解决方案

#1


3  

Have you checked JSONLib? It doesn't do exactly what you're looking for though. But it is close.

你检查过JSONLib了吗?它并没有完全符合您的要求。但它很接近。

#2


2  

Is this what you're looking for? http://www.json.org/java/

这是你在找什么? http://www.json.org/java/

#3


1  

Not straightforward, but I'd combine JAXB, Jackson and BeanUtils.

不是直截了当,但我结合了JAXB,Jackson和BeanUtils。

Here's one part http://ondra.zizka.cz/stranky/programovani/java/jaxb-json-jackson-howto.texy

这是http://ondra.zizka.cz/stranky/programovani/java/jaxb-json-jackson-howto.texy的一部分

Here's the other... http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/expression/DefaultResolver.html

这是另一个...... http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/expression/DefaultResolver.html

#4


1  

Just came across your question, I believe this is a more standard option: https://jsonp.java.net/download.html Usage example: http://docs.oracle.com/javaee/7/api/javax/json/JsonObjectBuilder.html

刚刚遇到您的问题,我相信这是一个更标准的选项:https://jsonp.java.net/download.html用法示例:http://docs.oracle.com/javaee/7/api/javax/json /JsonObjectBuilder.html

#5


0  

I realise this is an older question, but I have implemented a really simple JSON String Builder in C# as part of my Fluent Flot project which could be quite easily ported.

我意识到这是一个较老的问题,但我在C#中实现了一个非常简单的JSON字符串生成器,作为我的Fluent Flot项目的一部分,可以很容易地移植。

#6


0  

Elasticsearch has a very good fluent JSON builder; unfortunately, as far as I can tell, it's not a module but part of the bulk of the elasticsearch codebase, so it's not easily used without elasticsearch.

Elasticsearch有一个非常好的流畅的JSON构建器;不幸的是,据我所知,它不是一个模块,而是弹性搜索代码库的一部分,所以没有弹性搜索它就不容易使用。

See for an example: http://www.elasticsearch.org/guide/reference/java-api/index_.html

请参阅示例:http://www.elasticsearch.org/guide/reference/java-api/index_.html

#7


0  

A bit late with this but my jsonj library was designed for this usecase.

有点晚了,但我的jsonj库是为这个用例而设计的。

https://github.com/jillesvangurp/jsonj

JsonObject o=object(
    field("aList",array(
        1,
        2,
        object(field("meaningoflife",42)),
        "no more builder"))
    ),
    field("another", "element"),
    field("aSet",set(1,2,3),
    field("nestedlists",array(
       array(1,2),
       array(3,4)
     ))
);

String json=o.toString();

I use several static methods that you can statically import. The builder methods are polymorph, and use varargs. JsonObject, JsonArray, JsonSet, and JsonPrimitive behave in sane ways and support generics as well.

我使用了几种静态方法,你可以静态导入。构建器方法是多态的,并使用varargs。 JsonObject,JsonArray,JsonSet和JsonPrimitive也以理智的方式运行并支持泛型。

#1


3  

Have you checked JSONLib? It doesn't do exactly what you're looking for though. But it is close.

你检查过JSONLib了吗?它并没有完全符合您的要求。但它很接近。

#2


2  

Is this what you're looking for? http://www.json.org/java/

这是你在找什么? http://www.json.org/java/

#3


1  

Not straightforward, but I'd combine JAXB, Jackson and BeanUtils.

不是直截了当,但我结合了JAXB,Jackson和BeanUtils。

Here's one part http://ondra.zizka.cz/stranky/programovani/java/jaxb-json-jackson-howto.texy

这是http://ondra.zizka.cz/stranky/programovani/java/jaxb-json-jackson-howto.texy的一部分

Here's the other... http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/expression/DefaultResolver.html

这是另一个...... http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/expression/DefaultResolver.html

#4


1  

Just came across your question, I believe this is a more standard option: https://jsonp.java.net/download.html Usage example: http://docs.oracle.com/javaee/7/api/javax/json/JsonObjectBuilder.html

刚刚遇到您的问题,我相信这是一个更标准的选项:https://jsonp.java.net/download.html用法示例:http://docs.oracle.com/javaee/7/api/javax/json /JsonObjectBuilder.html

#5


0  

I realise this is an older question, but I have implemented a really simple JSON String Builder in C# as part of my Fluent Flot project which could be quite easily ported.

我意识到这是一个较老的问题,但我在C#中实现了一个非常简单的JSON字符串生成器,作为我的Fluent Flot项目的一部分,可以很容易地移植。

#6


0  

Elasticsearch has a very good fluent JSON builder; unfortunately, as far as I can tell, it's not a module but part of the bulk of the elasticsearch codebase, so it's not easily used without elasticsearch.

Elasticsearch有一个非常好的流畅的JSON构建器;不幸的是,据我所知,它不是一个模块,而是弹性搜索代码库的一部分,所以没有弹性搜索它就不容易使用。

See for an example: http://www.elasticsearch.org/guide/reference/java-api/index_.html

请参阅示例:http://www.elasticsearch.org/guide/reference/java-api/index_.html

#7


0  

A bit late with this but my jsonj library was designed for this usecase.

有点晚了,但我的jsonj库是为这个用例而设计的。

https://github.com/jillesvangurp/jsonj

JsonObject o=object(
    field("aList",array(
        1,
        2,
        object(field("meaningoflife",42)),
        "no more builder"))
    ),
    field("another", "element"),
    field("aSet",set(1,2,3),
    field("nestedlists",array(
       array(1,2),
       array(3,4)
     ))
);

String json=o.toString();

I use several static methods that you can statically import. The builder methods are polymorph, and use varargs. JsonObject, JsonArray, JsonSet, and JsonPrimitive behave in sane ways and support generics as well.

我使用了几种静态方法,你可以静态导入。构建器方法是多态的,并使用varargs。 JsonObject,JsonArray,JsonSet和JsonPrimitive也以理智的方式运行并支持泛型。