I have a json object which will be returned from the server side as follows.
{"name":"value which has \" "} for Ex : {"Key":"This Key\" "}
when i get this response on the client side it is automatically encoded as , result after stringify
{"Key":"This Key\\\" "}
Now i want to replace the \\\"
to only \"
so my UI can show only This Key"
Until i tried to do jsonString.replace(/\\\"/g,'\"');
but gives output of This Key\\"
Kindly help me, i have got it wrong..
Regards, Punith
I have a json object which will be returned from the server side as follows.
{"name":"value which has \" "} for Ex : {"Key":"This Key\" "}
when i get this response on the client side it is automatically encoded as , result after stringify
{"Key":"This Key\\\" "}
Now i want to replace the \\\"
to only \"
so my UI can show only This Key"
Until i tried to do jsonString.replace(/\\\"/g,'\"');
but gives output of This Key\\"
Kindly help me, i have got it wrong..
Regards, Punith
Share Improve this question asked Mar 14, 2013 at 12:27 Punith RajPunith Raj 2,1956 gold badges27 silver badges48 bronze badges 9- The server response doesn't need any transformations. It's valid JSON. – Ja͢ck Commented Mar 14, 2013 at 12:30
- What exactly are you doing with the string? Sounds like you're double-escaping somewhere needlessly. – deceze ♦ Commented Mar 14, 2013 at 12:31
- @jack yes its a valid JSON, but my UI is showing it as THIS KEY\" ..... but i want it as THIS KEY" – Punith Raj Commented Mar 14, 2013 at 12:31
- @deceze THIS KEY" is the value stored in Database, on retrieve and kept it in java String its escaped to THIS KEY\" and then when the object is converted to JSON it is now THIS KEY\\\" – Punith Raj Commented Mar 14, 2013 at 12:33
- str.replace('\\\\"','\"'); worked. please leave your ment if there is any better ways. – Punith Raj Commented Mar 14, 2013 at 12:38
2 Answers
Reset to default 4You appear to be trying to write a JSON parser out of regular expressions. Don't do that, use an existing one.
var data = JSON.parse(string_of_json);
var key = data.Key;
You can use replace() function:
str.replace('\\\\"','\"');
It works.
P.S. You have forget a "\"