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.
\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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With