Json parse error with double quotes

A double quote, even if escaped, throws a parse error.
look at the code below

//parse the json in javascript var testJson = '{"result": ["lunch", "\"Show\""] }'; var tags = JSON.parse(testJson); alert (tags.result[1]); 

This throws a parsing error due to double quotes (which are already escaped).
Even eval() will not work here.
But if I go away with double slashes, for example:

 var result = '{"result": ["lunch", "\\"Show\\""] }'; var tags = JSON.parse(result); alert (tags.result[1]); 

then it works great.
Why do we need to use double slash in javascript? The problem is that the PHP function json_encode() has a double quote with a single slash (for example: \"show\" ) that JSON.parse cannot JSON.parse . How can I deal with this situation?

+42
json javascript php parsing double-quotes
Jun 04 '09 at 9:58
source share
6 answers

Well, finally, JSON analysis uses the same eval, so there is no difference when you give them smth. with the wrong syntax. In this case, you need to convince your quotes in php correctly, and then avoid them and dump them using json_encode

 <?php $json = '{"result": ["lunch", "\"Show\""] }'; echo json_encode($json); ?> OUTPUT: "{\"result\": [\"lunch\", \"\\\"Show\\\"\"] }" 

This should work on the client side of JS (unless I made typos).

+29
Jun 04 '09 at 10:20
source share

Javascript unescapes their lines and json unescapes them as well. the first line ( '{"result": ["lunch", "\"Show\""] }' ) is considered by the json parser as {"result": ["lunch", ""Show""] } , because \" in javascript means " but does not go out of line with double quotes.

The second line is '{"result": ["lunch", "\\\"Show\\\""] }' not first tied to {"result": ["lunch", "\"Show\""] } (and this is incorrectly canceled by json).

I think that '{"result": ["lunch", "\\"Show\\""] }' should work too.

+33
Jun 04 '09 at 10:11
source share

This problem is caused by a two-line mechanism: one from JS and one from JSON.

The combination of a backslash character in combination with another next character is used to represent a single character that is not otherwise represented in the string. '' \\ '' means '\' etc.

This escaping mechanism occurs before JSON.parse () works.

For example,

 var parsedObj = JSON.parse('{"sentence": "It is one backslash(\\\\)"}'); console.log(parsedObj.sentence); >>>"It is one backslash(\)" 

From the point of view of the line generator, it passes four '\' gaps to the JavaScript interpreter.

From the perspective of the JavaScript interpreter, it assumes that it has two attenuations (\), since each sequence "\\" will be interpreted as "\".

From the point of view of the JSON parser, it gets two gaps (\\), and the JSON string escape sequence rules will parse it as one single '\', which is the result of the output.

Explain the first code:

 var testJson = '{"result": ["lunch", "\"Show\""] }'; //The real string after sequence escaping in to JS is //'{"result": ["lunch", ""Show""] }' //which is passed into the JSON.parse. //Thus, it breaks the JSON grammar and generates an error var tags = JSON.parse(testJson); alert (tags.result[1]); 
+10
Oct 26 '11 at 15:09
source share

From docs

JSON_HEX_APOS (integer) All 'are converted to \ u0027
JSON_HEX_QUOT (integer) All "are converted to \ u0022

json_encode () takes two arguments, a value and parameters. Therefore try

 json_encode($result, JSON_HEX_QUOT); // or json_encode($result, JSON_HEX_QUOT | JSON_HEX_APOS); 

I have not tried this though.

+8
Jun 04 '09 at 10:14
source share

Disable magic_quotes_gpc in php.ini .

+2
Apr 22 '10 at 8:53
source share

php to javascript Object (php> = 5.3.0)

 var storesLocations = JSON.parse('<?php echo addslashes(json_encode($storesLocations,JSON_HEX_APOS | JSON_HEX_QUOT)) ?>'); 
+1
02 Sep '14 at 11:26
source share



All Articles