50

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)

starball
  • 20,030
  • 7
  • 43
  • 238
Mister Smith
  • 27,417
  • 21
  • 110
  • 193

6 Answers6

10

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.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • It's very weird how this is still happening!! Anywho, I did a String replace on the json string and got the resulting string and passed that to the StringEntity. It worked like magic. Thanks and cheers!! – Migisha Nov 14 '14 at 19:09
  • My gess is: `json.replace("\\\\", "\\")` – Grigory Kislin Jul 19 '21 at 20:45
7
jsonObjSend.toString().replace("\\\\","")

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

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Vinay
  • 1,859
  • 22
  • 26
6

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

It should be possible to copy class JSONStringer and 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.

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

devconsole
  • 7,875
  • 1
  • 34
  • 42
  • 1
    Maybe another JSON library will also do what you want, see [google-gson](http://code.google.com/p/google-gson/) for example. – devconsole May 15 '13 at 12:06
  • 2
    You are right. The `JsonWriter.setHtmlSafe` method seems to do exactly that. The default setting is false, so I guess this library produces unscaped JSON strings. I don't have the time to test it, but if you did I'd accept this as solution :) – Mister Smith May 15 '13 at 12:22
3

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.

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);
MoleIsKing
  • 131
  • 5
0

I was getting similar slashes when I used

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

You need to deserialize this json.

JSONObject(dateString)
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
-5

The problem is with the imports.

Use below imports :-

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

Instead of import org.json.simple.JSONObject;

It will work.

See image

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
  • Hm, no, I'm definitely importing `org.json.JSONObject` and not the "simple" variant, and I have these dumb escaped slashes. :( – Wilmer Aug 03 '23 at 12:59