Monday, July 30, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...