简体   繁体   English

Servlet JSON响应编码问题“ =”

[英]Servlet JSON response ENCODING issue For “=”

I have simple JSON Object as follows : 我有以下简单的JSON对象:

{"status":"Success","action":"Redirect","sessionid":6467349943156736,"url":"https://myapplicationing.com/go?id=1000"}

i have created JSON as follows: 我创建了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");

when i write this json as response 当我写这个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();
        }

But it is still giving me JSON string as follows : 但是它仍然给我JSON字符串,如下所示:

{"status":"Success","action":"Redirect","sessionid":6467349943156736,"url":"https://myapplicationing.com/go?id\u003d1000"}

Here why it is ENCODING JSON String. 这就是为什么它ENCODING JSON字符串。 It is replacing "=" to "\=". 它将“ =”替换为“ \\ u003d”。

I have tried this one : 我已经尝试过这个了:

Gson gson = new GsonBuilder().disableHtmlEscaping().create();
resp.getWriter().println(gson.toJson(json));

But not working. 但是没有用。 Any Solution on this. 任何解决方案。

\= is the unicode represention of = . \==的unicode表示。 It can be converted back to = when parsing the JSON. 解析JSON时可以将其转换回=

Have a look at http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringEscapeUtils.html to convert unicode characters to normal string. 看看http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringEscapeUtils.html将Unicode字符转换为普通字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM