简体   繁体   English

JSONObject.toString:如何不转义斜杠

[英]JSONObject.toString: how NOT to escape slashes

I need to send a date in JSON. The date string should look like this:我需要发送 JSON 中的日期。日期字符串应如下所示:

"2013/5/15" “2013/5/15”

Instead, JSONObject.toString escapes 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.replace after 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?有没有办法序列化 JSON object 没有 escaping 斜线? (If possible, without escaping anything) (如果可能的话,没有 escaping 任何东西)

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. 幸运的是,JSONObject还转义了反斜杠,因此我也必须取消转义。 Now if I wanted to send "\\ /" intentionally the escaped string would be "\\\\/" and the result of replacing is the original string as intended. 现在,如果我想有意发送“ \\ /”,则转义的字符串将为“ \\\\ /”,并且替换的结果是预期的原始字符串。

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 JSONStringer and implement your own version of value(Object) (line 227+). 应该可以复制类JSONStringer并实现您自己的value(Object)版本(第227+行)。 Then implement your own version of JSONObject .toString() in a utility class and use your own JSONStringer instead of the original. 然后在实用程序类中实现您自己的JSONObject .toString()版本,并使用自己的JSONStringer而不是原始版本。

EDIT: Subclassing JSONStringer won't be easy because value() calls a private method beforeValue() that cannot be accessed. 编辑:子类化JSONStringer并不容易,因为value()调用了无法访问的私有方法beforeValue()。

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

Worked for me. 为我工作。 A bit dirty trick but seems no other solution. 有点肮脏的把戏,但似乎没有其他解决方案。

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,.....". 在处理编码为adat Uri“ data:image / png; base64,.....”的图像的数据时,JSONObject“ put”遇到了类似的问题。 The put function would add another slash to change the format to "data:image/png;base64,.....". put函数将添加另一个斜杠以将格式更改为“ 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. 看来问题的根源是JSONObject“ put”函数中的字符串值检查,该函数添加了额外的斜杠。 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. 一种可能是重载该函数或扩展该类,但我发现最简单的方法是添加一个唯一的字符串(例如guid),然后在调用JSONObject的toString()函数之后用您的Uri字符串替换该guid。

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);

I was getting similar slashes when I used当我使用时,我得到了类似的斜线

val dateString = Gson().toJson(dateObject).toString()

You need to deserialize this json.你需要反序列化这个 json。

JSONObject(dateString)

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. 会的。

看图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM