java 是否存在这样的 JSON 字符串构建器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3081715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does such a JSON string builder exist?
提问by digiarnie
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"}}
采纳答案by Behrang Saeedzadeh
回答by Ben J
Is this what you're looking for? http://www.json.org/java/
这是你要找的吗?http://www.json.org/java/
回答by Alexander
Just came across your question, I believe this is a more standard option: https://jsonp.java.net/download.htmlUsage 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
回答by Ondra ?i?ka
Not straightforward, but I'd combine JAXB, Hymanson and BeanUtils.
不简单,但我会结合 JAXB、Hymanson 和 BeanUtils。
Here's one part http://ondra.zizka.cz/stranky/programovani/java/jaxb-json-Hymanson-howto.texy
这是一部分 http://ondra.zizka.cz/stranky/programovani/java/jaxb-json-Hymanson-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
回答by chris raethke
I realise this is an older question, but I have implemented a really simple JSON String Builder in C#as part of my Fluent Flotproject which could be quite easily ported.
我意识到这是一个较旧的问题,但我已经在 C# 中实现了一个非常简单的JSON String Builder作为我的Fluent Flot项目的一部分,它可以很容易地移植。
回答by Eugen
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 builder;不幸的是,据我所知,它不是一个模块,而是大部分 elasticsearch 代码库的一部分,因此如果没有 elasticsearch,它就不容易使用。
See for an example: http://www.elasticsearch.org/guide/reference/java-api/index_.html
参见示例:http: //www.elasticsearch.org/guide/reference/java-api/index_.html
回答by Jilles van Gurp
A bit late with this but my jsonj library was designed for this usecase.
这有点晚了,但我的 jsonj 库是为这个用例设计的。
https://github.com/jillesvangurp/jsonj
https://github.com/jilesvangurp/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.
我使用了几个可以静态导入的静态方法。构建器方法是多态的,并使用可变参数。JsonObject、JsonArray、JsonSet 和 JsonPrimitive 以正常的方式运行并支持泛型。

