Overview of the effect of the escape character (\) on JSON.parse in JavaScript

Effect of escape character (\) on JSON.parse in JavaScript

According to the explanation in the fifth edition of ECMA262, JSON is a built-in object that provides stringify and parse methods. The former is used to convert js objects into json-compliant strings, and the latter converts json-compliant strings into js objects. . json standard reference <a href="http://json.org/" rel="external nofollow" target="_blank" target="_blank">json.org</a>. (In fact, eval can be used to convert a string that meets the json standard into a js object, but eval performance is relatively poor and there are security risks (the code in the json string will be executed). This article only writes JSON)

What effect does the escape character (\) have on the JSON.parse method?

Generally speaking, when the parameters of JSON.parse contain shifting characters, you will encounter the problem of escaping twice. In fact, the first time is the escaping of the string itself, and the second time is the escaping of the actual js object.

Examples are as follows:

Example one:

Pass the string '{"a":"b","b":"\\\\"}' to JSON.parse, first the parser considers the first \ to escape when extracting the string enclosed in single quotes The second \ third \ escapes the fourth \, that is to say, the actual output string is {"a":"b","b":"\\"} (via console.log(' {"a":"b","b":"\\\\"}') verification), and then there is another escape when it is officially converted to a js object, which is the first one in the actual output character conversion \ escapes the second \ (there are only two \ at this point). So console.log(JSON.parse('{"a":"b","b":"\\\\"}') ); the output is Object {a: "b", b: "\" }, that is to say, the actual displayed data is a \ (the actual output of a \ indicates that there is a \ before this).


Example two:

[javascript] 

var obj = {  
    a : "b",  
    b : "\\",  
    c : {  
        b : "\\",   
        a : {   
            b : "\\"   
        }  
    }  
};  
var json_str = JSON.stringify(obj);  
console.log( JSON.stringify(obj) );  
console.dir(JSON.parse(json_str));  
console.dir(JSON.parse('{"a":"b","b":"\\\\","c":{"b":"\\\\","a":{"b":"\\\\"}}}'));  

The output result is as follows:

According to the escaping rules, the actual output of a \ must have a \ before this \. So the output of the first line above is written as '{"a":"b","b":"\\\\","c":{"b":"\\\\","a ":{"b":"\\\\"}}}', which can be verified by the third output.

Summary: If you want one \ to appear in the js object, you need to appear four \ in the json string.

For other special characters:

1. Double quotation mark ("), if the double quotation mark appears correctly, it should be \\\"
2.\n, if you think about the correct newline, you need \\n in the json string, in fact, you must first correct the \ in \n Escaping, n becomes an ordinary character, and n and the previous \ (only one \) are interpreted as a newline when parsed into a js object. The following two are similar to this.
3. \r, \\r
4. \t, \\t

 

Guess you like

Origin blog.csdn.net/cdming/article/details/130300309