12
有人可以請建議爲什麼會發生這種情況...使用Gson漂亮打印JSON字符串的問題
我有一些代碼來打印一些JSON。要做到這一點,我正在使用Gson library。
但是,雖然通常效果不錯,但某些字符看起來並沒有正確顯示。下面是一段簡單的代碼演示了此問題:
//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)"
}
}
我怎麼能解決上面?