Skip to content Skip to sidebar Skip to footer

Json Javascript Escape

So i have some of my example dynamic JSON below, what i'm having trouble doing is escaping everything properly so that it is properly processed by JSON.parse or Jquery.parseJSON, w

Solution 1:

Inside JSON, quotes within strings need to be escaped with a backslash: {"key": "prop with \" quote"}.

Inside JavaScript, quotes and backslashes within string literals need to be escaped with a backslash: "string with \\backslash and \" quote".

If you really would need to use JSON in JS string literals (there is no reason to do that), you would need to double-escape them: json = "{\"key":\"prop with \\\" quote and \\n linebreak\"}". You haven't done so for the quotes around "Windows Phone".

However, you must've done something wrong when dealing with such problems. Usually you get JSON strings from ajax calls and such, where you get them already as a string value. If you want to echo some sever-created JSON directly into a js script, you don't need to wrap it in a string literal - it is already [nearly] valid Object Literal syntax.

Solution 2:

Your problem is probably that your whole Json object is just one string, because of the quotes on the start and end. The idea of JSON is assigning complex variables to one object, like this:

var Json = {
  "resolved_id": "244296544",
  ...
}

Also, there is no need to escape forward slashes.

Solution 3:

All the way at the end:

'...\n\n"}';

Escape the backslashes:

'...\\n\\n"}';

Solution 4:

According to JSONLint, your problem is on this line:

"title": "Windows Phone 7 Connector for Mac updated for WP8, rebranded simply as \'Windows Phone\'",

If you remove the backslashes on ', it validates. In JSON, you don't escape '

Unfortunately, since you are using ' to delimit your string, you'll need to find another way to escape it. You could use \u0027 in place of \'.

Post a Comment for "Json Javascript Escape"