我有如下简单的JSON对象:
{"status":"Success","action":"Redirect","sessionid":6467349943156736,"url":"https://myapplicationing.com/go?id=1000"}
我按如下方式创建了JSON:
JSONObject json = new JSONObject();
json.put("status", "Success");
json.put("action", "Redirect");
json.put("sessionid", "6467349943156736");
json.put("url", "https://myapplicationing.com/go?id=1000");
当我把这个json写为回复时
resp.setContentType("application/json");
resp.setHeader("Cache-Control", "no-cache");
resp.setCharacterEncoding("utf-8");
try {
// json.write(resp.getWriter());//[tried]
// Gson gson = new GsonBuilder().disableHtmlEscaping().create();
resp.getWriter().println(json.toString());
// resp.getWriter().println(gson.toJson(json));//[TRIED]
} catch (Exception e) {
e.printStackTrace();
}
但它仍然给我JSON字符串如下:
{"status":"Success","action":"Redirect","sessionid":6467349943156736,"url":"https://myapplicationing.com/go?id\u003d1000"}
这是为什么它是ENCODING
JSON字符串。
它将“=”替换为“\ u003d”。
我试过这个:
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
resp.getWriter().println(gson.toJson(json));
但不行。对此有任何解决方案。
答案 0 :(得分:1)
\u003d
是=
的unicode表示形式。解析JSON时,它可以转换回=
。
查看http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringEscapeUtils.html将unicode字符转换为普通字符串。