2

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

9
  • The server response doesn't need any transformations. It's valid JSON. Commented Mar 14, 2013 at 12:30
  • What exactly are you doing with the string? Sounds like you're double-escaping somewhere needlessly. 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" 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\\\" Commented Mar 14, 2013 at 12:33
  • str.replace('\\\\"','\"'); worked. please leave your comment if there is any better ways. Commented Mar 14, 2013 at 12:38

2 Answers 2

4

You 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;
Sign up to request clarification or add additional context in comments.

2 Comments

But only reason i refrained from this is i had to download another JS file, and the project we cannot use any opensource library.
@PunithRaj — JSON.parse is built into modern browsers. Extjs (which you appear to be using already) has its own JSON parser.
2

You can use replace() function:

str.replace('\\\\"','\"');

It works.

P.S. You have forget a "\"

1 Comment

Good :) Check this as the answer ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.