Monday 30 July 2018

java - JSONObject.put(String) adds '' for each '/'





I'm just recogniced that this code:



try {
String jsonString =new JSONObject().put("test","Ha/llo").toString();
} catch (JSONException e) {

e.printStackTrace();
}


ouputs the following:



{"test":"Ha\ /llo"}



Does someone knows why it puts an \ for each / ? And how to get the real String?
My Strings i want to put are supposed to be big, so I dont want to search for \ to change it to ' '




Edit: .get decodes it again and removes the extra '\' in my case is was an server side problem.


Answer



It is escaping the / character automatically. When a valid JSON client parses your string, it should unescape it, resulting in no issues and the original text.
See JSON: why are forward slashes escaped?


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...