Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

java - JSONObject.toString: how NOT to escape slashes

I need to send a date in JSON. The date string should look like this:

"2013/5/15"

Instead , JSONObject.toString escapes it as follows:

"2013 /5 /15"

I understand that this is done to allow json strings inside scripts tags, as this question explains: JSON: why are forward slashes escaped?

But in my case I don't need it. In fact the server is returning an error. The server is not dealing with this and I can't fix the server, so I must fix it in the mobile client code.

I could do a String.replace after serializing it, but what if I actually wanted to include the " /" string in any other part of the JSON?

Is there a way to serialize a JSON object without escaping slashes? (If possible, without escaping anything)

Thanks in advance.

question from:https://stackoverflow.com/questions/16563579/jsonobject-tostring-how-not-to-escape-slashes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally opted for the quick and dirty trick of replacing the escaped slashes in the serialized string before sending it to the server. Luckily, JSONObject also escapes backslashes, so i must also unscape them. Now if I wanted to send " /" intentionally the escaped string would be "\/" and the result of replacing is the original string as intended.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...