//Creating the JSON object, and getting as String: JsonObject json = new JsonObject(); JsonObject inner = new JsonObject(); inner.addProperty("value", "xpath('hello')"); json.add("root", inner); System.out.println(json.toString()); //Trying to pretify JSON String: Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser parser = new JsonParser(); JsonElement je = parser.parse(json.toString()); System.out.println(gson.toJson(je));
输出{"root":{"value":"xpath('hello')"}} { "root": { "value": "xpath(\u0027hello\u0027)" }
}
解决方案初始化的时候
Gson gs = new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.create();
本文介绍如何使用Gson库美化JSON字符串的输出格式,并禁用HTML转义功能,确保特殊字符如单引号能正确显示。
910

被折叠的 条评论
为什么被折叠?



