Jsonobject.Tostring: How Not to Escape Slashes

Send Nested JSON Object without Escape Character

This is your error:

paramsForJSON.put("user", userJSONObject.toString().replaceAll("\\\\", ""));

You've turned the user into a String you don't need to, just do this:

loginRequestJSONObject.put("user", userJSONObject);

Though you've already done this, you've actually got the correct lines in there, this is all you need:

final JSONObject loginRequestJSONObject = new JSONObject();
final JSONObject userJSONObject = new JSONObject();
userJSONObject.put("login", "myuser");
userJSONObject.put("password", "mypass");

loginRequestJSONObject.put("user", userJSONObject);
loginRequestJSONObject.put("commit", "Sign In");

JSONObject objectToSend = loginRequestJSONObject;

JSONObject contains escape characters

JSONObject is correctly encoding the string. This page describes how string literals are to be escaped in JavaScript (and, by extension, JSON). The following note is important to understanding what happens in your example:

For characters not listed in Table 2.1, a preceding backslash is ignored, but this usage is deprecated and should be avoided.

Your example ("\/Date(1382459367723)\/") uses a preceding backslash before a /. Because / is not in table 2.1, the \ should simply be ignored. If your service doesn't ignore the \, then it either has a bug, or is not a JSON parser (perhaps it uses a data format which is similar to, but not quite, JSON).

Since you need to generate non-conforming JSON, you won't be able to use standard tools to do so. Your two options are to write your own not-quite-JSON encoder, or to avoid characters which must be escaped, such as \ and ".

JSONObject.put(String) adds '\' for each '/'

It is escaping the / character automatically. When a valid JSON client parses your string, it should unescape it, resulting in no issues and the original text.
See JSON: why are forward slashes escaped?

How can I disable Scala Play json's backslash escape in `JsObject.toString()`?

"abc\edf" is not valid JSON, as per the JSON specs:
https://www.json.org/json-en.html

\ can only be followed by ", \, /, b, f, n, r, t, or u+4 hexadecimal digits representing a Unicode code point.

Sample Image

Play Framework simply isn't allowing you to output malformed JSON.

More specifically, a single \ must be escaped in JSON to \\. But if you parse the JSON back into Scala, it'll get unescaped back to \.

val in = """abc\edf"""
val out = Json.parse(Json.stringify(JsString(in))).as[String]

out should look like this abc\edf.

To extract a specific property out of an object, use Play's JSON traversal mechanism.

scala> val obj = JsObject(Seq("abc" -> JsObject(Seq("edf" -> JsString("""111\2222""")))))
val obj: play.api.libs.json.JsObject = {"abc":{"edf":"111\\2222"}}

scala> (obj \ "abc" \ "edf").as[String]
val res0: String = 111\2222

How to avoid backslashes in GSON JsonObject?

The problem is caused by this:

tjsonObject.addProperty("channels", gson.toJson(channelsList));

What that is doing is converting channelsList to a string containing a representation of the list in JSON, then setting the property to that string. Since the string contains JSON meta-characters, they must be escaped when the strings are serialized ... a second time.

I think that you need to do this instead:

tjsonObject.add("channels", gson.toJsonTree(channelsList));

That should produce this:

{
"tags": {
"channels":
[{"type":"channel","id":"channel","name":"Channel","parent":"SXM"}],
"shows":
[{"type":"shows","id":"shows","name":"Shows","parent":"SXM"},
{"type":"shows","id":"howard","name":"Howard Stern","parent":"shows"}
....

That is slightly different to what your question asked for, but it has the advantage of being syntactically valid JSON!

JSON adding backslash automatically

You've already converted it to a JSON string, then you stick that into a JSON object which automatically escapes the JSON string. Try something like this:

User u=new User(name,lastName,email,phone,password);
Gson userGson=new GsonBuilder().create();
JSONObject params = new JSONObject();
params.put("user",user);
Log.e("in register", userGson.toJson(params));


Related Topics



Leave a reply



Submit