在填充的JSONObject报价 [英] Padding quotes in JSONObject
问题描述
我建立一个JSON字符串以发送到我的web服务。由于小片的一个是用户输入的,有一个双引号的可能性。我试图通过转义它来解决这个问题。
I'm building a JSON string to send to my web service. Since one of the pieces is user-inputted, there is the possibility for double quotes. I'm trying to resolve the issue by escaping it.
String strValue = "height of 6\"";
JSONObject json = new JSONObject();
json.put("key", strValue.replaceAll("\"","\\\""));
这里的问题是,当我做 json.toString()
,我得到3斜杠。
例如:
{"key","height of 6\\\""}
如果我不尝试做任何更换, json.toString()
给我破JSON。
If I don't try to do any replacing, json.toString()
gives me broken json.
例如:
{"key", "height of 6""}
我怎样才能做到这一点是否正确?
How can I do this correctly?
请注意:当我的网站保存该值并显示,它显示的6 \\高度
Note: When my website saves this value and displays it, it displays height of 6\"
更新:
看来罪魁祸首就是 json.toString()
当我称之为的replaceAll
方法是 - 正确的 - 只逃脱双引号。看来 json.toString()
逃脱斜线。要解决这个问题,我必须做 json.toString()代替(\\\\\\\\,)
。这引出了一个问题:为什么在地球上做的JSONObject逃生斜线而不是双引号?????
When I call the replaceAll
method it -- correctly -- only escapes the double quote. It appears json.toString()
escapes slashes. To fix the issue, I must do json.toString().replace("\\\\", "")
. This begs the question: Why on Earth does JSONObject escape slashes and not double quotes?????
推荐答案
看来罪魁祸首就是 json.toString()
在我称之为的replaceAll方法是 - 正确的 - 只逃脱双引号。看来 json.toString()
逃脱斜线。要解决这个问题,我必须做 json.toString()代替(\\\\\\\\,)
。
When I call the replaceAll method it -- correctly -- only escapes the double quote. It appears json.toString()
escapes slashes. To fix the issue, I must do json.toString().replace("\\\\", "")
.
这引出了一个问题:为什么在地球上做的JSONObject逃生斜线而不是双引号?????
This begs the question: Why on Earth does JSONObject escape slashes and not double quotes?????
这篇关于在填充的JSONObject报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!