0

I have a JSON string hardcoded in my Javascript.

valiJsonString = '{"ssss","ddddddddd\"ddd"}';

The DOM says -> {"ssss","ddddddddd"ddd"}

Can someone tell me why javascript replace my \" into " ?

// try to parse
valiJsonString secureEvalJSON (valiJsonString)   //<-- error: jsonString is not valid

working example

4 Answers 4

2

"The DOM says" doesn't make much sense, as the DOM doesn't say anything. Do you mean the object browser in Firebug (or some other development console)?

Now, inside a string, \" is the quote character. You have to compensate for this escaping since you do not want it, but instead a verbatim slash.

So perhaps you want \\ followed by ", which is the slashed character followed by the quote character.

In addition, the given JSON looks like it ought to represent an array not an object, since you have no keys:

var str = '["ssss","ddddddddd\\"ddd"]';

The actual value of this JSON-format string inside your browser is now:

["ssss","ddddddddd\"ddd"]
Sign up to request clarification or add additional context in comments.

Comments

2

\ is an escape character. try \\

Comments

2

If you want your string to come through escaped, then you need to escape your escape character:

valiJsonString = '{"ssss","ddddddddd\\"ddd"}';

Comments

1

I've added second \ (\ is escape char) and fixed lack of = and type of table {} vs []

http://jsfiddle.net/4wVaR/9/

Comments

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.