JSONObject.toString:怎么不逃斜线 [英] JSONObject.toString: how NOT to escape slashes

查看:2440
本文介绍了JSONObject.toString:怎么不逃斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在JSON发送日期。日期字符串应该是这样的:

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

2013年5月15日

相反, JSONObject.toString 如下逃脱它:

2013 \\ / 10 \\ / 15

"2013\ /5\ /15"

据我了解,这样做是为了让里面的脚本标记JSON字符串,如这一问题解释说:
JSON:为什么斜杠转义

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

但在我的情况下,我并不需要它。事实上,服务器返回一个错误。服务器不处理这一点,我不能修复的服务器上,所以我必须在移动客户端code修复它。

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.

我可以序列化后做了与string.replace ,但如果我其实是想包括在JSON的任何其他部分的\\ /字符串?

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?

有没有办法序列化JSON对象没有逃脱斜线? (如果可能的话,没有任何转义)

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

先谢谢了。

推荐答案

我最终选择将其发送到服务器之前在序列化的字符串替换逃脱斜线的快速和肮脏的把戏。幸运的是,同样的JSONObject逃脱反斜杠,所以我也必须unscape他们。现在,如果我想送\\ /故意逃脱的字符串将是\\\\ /和更换的结果是按照预期的原始字符串。

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.

这篇关于JSONObject.toString:怎么不逃斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆