//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();