How to escape "\" from JSON string in Titanium? - json

I have written a native android module where i am returning Json string to JavaScript layer. But in javascript layer , i am getting this json string added with "\" for all "". How to escape this "\" from json string.

Seems that your json is not a "real JSON object" but just a String. This explain why " symbol are escaped. Try to use JSON.parse() in your javascript.
var my_json_string = "{\"my_key\": \"the_value\"}";
var my_json_object = JSON.parse(my_json_string);
// should render something like
// Object {my_key: "the_value"}

Related

How to include a JSON encoded object as string in another object which will also be encoded in string

Suppose you have the following JSON object:
{"name":"John Smith",
"jsonData": "{\"comment\":\"He said \\\"It will work\\\", and we are waiting.\"}"
}
The question is how to convert the above object to a string using say JavaScript JSON.stringify() and be able to save it in a text field in the DB and retrieve it later, parse it, and also part the inner encoded object jsonData?
The problem is faced in JavaScript and Java. For simplicity, I will reproduce the problem in JavaScript:
var jsonStr = `{"name":"John Smith",
"jsonData": "{\\\"comment\\\":\\\"He said \\\"It will work\\\", and we are waiting.\\\"}"
}`;
var obj = JSON.parse(jsonStr);
var comments = JSON.parse(obj.jsonData);
console.log(comments)
The above is failing with the error: Uncaught SyntaxError: Unexpected token I in JSON at position 21
And, I couldn't figure out how to include a quoted string in the inner encoded JSON string He said "It will work".
I want the solution in both Java and JavaScript.
Have you considered base64 encoding the JSON string?
Read more about base64 encoding objects here:
Base64 encode a javascript object

Enumerating of JObject of NewtonSoft.Json loses '\' character in C#

I would like to parse json string using JObject.Parse() of NewtonSoft.Json. Assume that the json string is like this:
{"json":"{\"count\":\"123\"}"}
The result of jObject.First.ToString() is "json": "{\"count\":\"123\"}".
The result of jObject["json"].ToString() is {"count":"123"}. Enumerating gets the same result as this.
The testing code I used is like this.
[TestMethod()]
public void JsonParseTest()
{
var json = "{\"json\":\"{\\\"count\\\":\\\"123\\\"}\"}";
var jObject = JObject.Parse(json);
Console.WriteLine($"json : {json}");
Console.WriteLine($"jObject.First.ToString() : {jObject.First}");
Console.WriteLine($"jObject[\"json\"].ToString() : {jObject["json"]}");
}
We can see that enumerating of jObject will lose the character '\'. What is the problem? I would be appreciated for any suggestion :)
EDIT 1
The version of NewtonSoft is 12.0.3 released in 2019.11.09.
The parser isn't loosing anything. There is no literal \ in your example. The backslashes are purely part of the JSON syntax to escape the " inside the string vlue. The value of the key json is {"count":"123"}.
If you want to have backslashes in that value (however I don't see why you would want that), then you need add them, just like you added them in your C# string (C# and JSON happen to have the same escaping mechanism):
{"json":"{\\\"count\\\":\\\"123\\\"}"}
with leads to the C# code:
var json = "{\"json\":\"{\\\\\\\"count\\\\\\\":\\\\\\\"123\\\\\\\"}\"}";

How to remove backslash from JSON string using object mapper

Here is the code of getting Json string from object using object mapper:
let jsonString = Mapper().toJSONString(freechargeRequest)
but the output is coming with backslash as follows:
"{\"region\":\"http:\/\/testrm.getquickride.com:8080\/dishaapiserver\/qr_freecharge_failure.do\",\"merchantId\":\"8mILp0KGOdEG57\",\"productInfo\":\"auth\",\"mobile\":\"1357924680\",\"channel\":\"IOS\",\"amount\":\"200\",\"surl\":\"http:\/\/testrm.getquickride.com:8080\/dishaapiserver\/qr_freecharge_success.do\",\"merchantTxnId\":\"510420180705115938\"}"
I wanted the output without backslashes in JSON string.

how to remove backslash added json.stringify

i'am currently working on a object to string conversion and upon reading i need to use json.stringify. I have successfully converted it, however when i try to input a backslash on my javascript object the conversion adds another backslash.
Scenario:
on my object variable i used "\r\n" after using the json.stringify the result was "\r\n" which should be only "\n" as i used the .replace function
var str = JSON.stringify(item.NotesContent);
var exp1 = str.replace(/\r\n/g, "\\\\n");
console.log("your new string is : " + exp1);
Result
any inputs for this?

JSON Object with Slashes in it

I am serializing to a json object using:
public static string ToJson(this object obj)
{
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
return jsonSerializer.Serialize(obj);
}
However when I'm populating a hidden field, I'm getting the slashes with it:
"[{\"ImageLink\":\"\",\"ShowOnHomePage\":null,\"Type\":\"AdListItem\",\"Key\":null,\"Title\":\"dsafdsaf\",\"Url\":\"fdsafdsa\",\"ContentSummary\":\"\u003cdiv\u003efdsafdsa\u003c/div\u003e\"},{\"ImageLink\":\"\",\"ShowOnHomePage\":null,\"Type\":\"AdListItem\",\"Key\":null,\"Title\":\"hddfg\",\"Url\":\"dsaf\",\"ContentSummary\":\"\u003cdiv\u003efdsafdsa\u003c/div\u003e\"},{\"ImageLink\":\"\",\"ShowOnHomePage\":null,\"Type\":\"AdListItem\",\"Key\":null,\"Title\":\"asfd\",\"Url\":\"asdf\",\"ContentSummary\":\"\u003cdiv\u003eafds\u003c/div\u003e\"}]"
How would I properly get rid of the \" and replace them with just " ???
Any Ideas?
Thanks.
The slashes are Javascript string escape characters.
\" -> " so you can have a quote with-in a quote.
This is true for most all C-style languages (C, C++, C#, Java, etc)