Java JSONObject.toString:如何不转义斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16563579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:26:26  来源:igfitidea点击:

JSONObject.toString: how NOT to escape slashes

javaandroidjson

提问by Mister Smith

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

我需要以 JSON 格式发送日期。日期字符串应如下所示:

"2013/5/15"

《2013/5/15》

Instead , JSONObject.toStringescapes it as follows:

相反,将其JSONObject.toString转义如下:

"2013\ /5\ /15"

《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?

我知道这样做是为了允许在脚本标签中使用 json 字符串,正如这个问题所解释的那样: JSON:为什么要转义正斜杠?

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.replaceafter serializing it, but what if I actually wanted to include the "\ /" string in any other part of the JSON?

我可以String.replace在序列化它之后做一个,但是如果我真的想在 JSON 的任何其他部分包含“\ /”字符串怎么办?

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

有没有办法在不转义斜杠的情况下序列化 JSON 对象?(如果可能,不要逃避任何事情)

Thanks in advance.

提前致谢。

采纳答案by Mister Smith

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 也可以转义反斜杠,所以我也必须对它们进行转义。现在,如果我想故意发送 "\ /" 转义字符串将是 "\\/" 并且替换的结果是预期的原始字符串。

回答by devconsole

That behavior is hard-coded into JSONStringer.java, see method private void string(String value), line 302+.

该行为被硬编码到JSONStringer.java 中,请参阅方法private void string(String value),第302+行。

It should be possible to copy class JSONStringerand implement your own version of value(Object)(line 227+). Then implement your own version of JSONObject.toString() in a utility class and use your own JSONStringer instead of the original.

应该可以复制类JSONStringer并实现您自己的版本value(Object)(第 227+ 行)。然后在实用程序类中实现您自己的JSONObject.toString()版本并使用您自己的 JSONStringer 而不是原始版本。

EDIT: Subclassing JSONStringer won't be easy because value() calls a private method beforeValue() that cannot be accessed.

编辑:子类化 JSONStringer 并不容易,因为 value() 调用了一个无法访问的私有方法 beforeValue()。

回答by Vinay

jsonObjSend.toString().replace("\\","")

Worked for me. A bit dirty trick but seems no other solution.

为我工作。有点肮脏的把戏,但似乎没有其他解决方案。

回答by MoleIsKing

I had a similar problem with JSONObject "put" when dealing with data for an image that was encoded into a adat Uri "data:image/png;base64,.....". The put function would add another slash to change the format to "data:image/png;base64,.....". It seems that the source of the problem is a string value check within the JSONObject "put" function that adds the extra slashs. One could maybe overload the function or extend the class but I found the easiest way is to add a unique string such as guid and then replace that guid with your Uri string after the calling the toString() function of your JSONObject.

在处理编码为 adat Uri "data:image/png;base64,....." 的图像的数据时,我在处理 JSONObject "put" 时遇到了类似的问题。put 函数会添加另一个斜杠以将格式更改为“data:image/png;base64,.....”。问题的根源似乎是 JSONObject“put”函数中的字符串值检查,它添加了额外的斜杠。人们可能会重载该函数或扩展该类,但我发现最简单的方法是添加一个唯一的字符串,例如 guid,然后在调用 JSONObject 的 toString() 函数后将该 guid 替换为您的 Uri 字符串。

JSONObject userJson = new JSONObject(); 
String myimageUri = "data:image/png;base64,XXXDATAXXX";
userJson.put("imageUri", "b0c8f13d-48b1-46b4-af28-4e2d8004a6f8");
userJson.toString().replace("b0c8f13d-48b1-46b4-af28-4e2d8004a6f8", myimageUri);

回答by Sachin Rai

The problem is with the imports.

问题在于进口。

Use below imports :-

使用以下进口:-

import org.json.JSONException;
import org.json.JSONObject; 

Instead of import org.json.simple.JSONObject;

代替 import org.json.simple.JSONObject;

It will work.

它会起作用。

See image

看图