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]);
steveyang Oct 26 '11 at 15:09 2011-10-26 15:09
source share