I'm using json-simple and I need to pretty-print JSON data (make it more human readable).
我在该库中找不到此功能. 这通常是如何实现的?
I'm using json-simple and I need to pretty-print JSON data (make it more human readable).
我在该库中找不到此功能. 这通常是如何实现的?
谷歌的GSON强可以很好地做到这一点:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJsonString);
String prettyJsonString = gson.toJson(je);
或者,由于现在建议使用JsonParser中的静态解析方法,您也可以使用以下方法:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement je = JsonParser.parseString(uglyJsonString);
String prettyJsonString = gson.toJson(je);
以下是导入语句:
import com.google.gson.*;
以下是Gradle依赖关系:
implementation 'com.google.code.gson:gson:2.8.7'