Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet JSON response ENCODING issue For "="

I have simple JSON Object as follows :

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

i have created JSON as follows:

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

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 :

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

Here why it is ENCODING JSON String. 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.

like image 755
Swap L Avatar asked Dec 04 '25 01:12

Swap L


1 Answers

\u003d is the unicode represention of =. It can be converted back to = when parsing the 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.

like image 68
anirudh Avatar answered Dec 05 '25 14:12

anirudh