美文网首页
Gson封装json例子以及特殊符号处理

Gson封装json例子以及特殊符号处理

作者: 小i柠檬 | 来源:发表于2016-12-31 15:29 被阅读0次
 //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例子以及特殊符号处理

      本文链接:https://www.haomeiwen.com/subject/tcavvttx.html