如何在Java中流畅地构建JSON? [英] How to fluently build JSON in Java?
本文介绍了如何在Java中流畅地构建JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想的是:
String json = new JsonBuilder()
.add("key1", "value1")
.add("key2", "value2")
.add("key3", new JsonBuilder()
.add("innerKey1", "value3"))
.toJson();
哪种Java JSON库最适合这种流畅的建筑?
Which Java JSON library is best for this kind of fluent building?
更新:我包装了GSON并得到了几乎所需的结果... 有一个故障。
Update: I wrapped GSON and got almost the desired result ... with one hitch.
推荐答案
我正在使用 org.json 库,发现它很友善。
I am using the org.json library and found it to be nice and friendly.
示例:
String jsonString = new JSONObject()
.put("JSON1", "Hello World!")
.put("JSON2", "Hello my World!")
.put("JSON3", new JSONObject()
.put("key1", "value1")).toString();
System.out.println(jsonString);
输出:
{"JSON2":"Hello my World!","JSON3":{"key1":"value1"},"JSON1":"Hello World!"}
这篇关于如何在Java中流畅地构建JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文