相关疑难解决方法(0)

使用Gson来打印JSON字符串的问题

有人可以建议为什么会发生这种情况......

我有一些代码可以打印一些JSON.为此,我正在使用Gson库.

但是,虽然通常效果很好,但某些字符似乎没有正确显示.这是一段简单的代码,演示了这个问题:

//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));
Run Code Online (Sandbox Code Playgroud)

上面代码的输出是:

{"root":{"value":"xpath('hello')"}}
{
  "root": {
    "value": "xpath(\u0027hello\u0027)"
  }
}
Run Code Online (Sandbox Code Playgroud)

我怎么能解决上述问题?

java json gson

17
推荐指数
1
解决办法
1万
查看次数

标签 统计

gson ×1

java ×1

json ×1