org.json

Doc: http://stleary.github.io/JSON-java/index.html

一个流行的JSON库,另一个流行的JSON库是 org.json.simple https://cliftonlabs.github.io/json-simple/ Maven仓库最后更新时间为2019年

Maven:

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>

JSONObject

1
2
3
myString = new JSONObject()
.put("JSON", "Hello, World!").toString();
// {"JSON": "Hello, World"}

Method

put(String key, value) 添加一个键值对

value 可以是 boolean Collection<?> double float int long Map<?,?> Object

toString() 返回无 whitespace 的JSON字符串

toString(int indentFactor) 返回 pretty-printed JSON 字符串
indentFactor 每个层级的缩进空格数