Escape character (\) Overview of the effects of Json.parse in JavaScript _ basics

Source: Internet
Author: User
Tags eval
As explained in the ECMA262 Fifth Edition, JSON is a built-in object that provides a stringify and parse method that converts a JS object to a JSON-standard string that converts a JSON-compliant string to a JS object. JSON standard reference <a href= "http://json.org/" target= "_blank" >json.org</a>. (In fact, converting a JSON-compliant string to a JS object can be done with eval, but the eval performance is relatively poor and there is a security risk (executing the code in the JSON string), this article writes only JSON)
This article is written about the effect of the escape character on the Json.parse method.

Generally speaking, when the Json.parse parameter contains the transfer character, you will encounter two escape problems, in fact, the first time is the escape of the string itself, the second is to really convert to JS object escape.

Examples are as follows:
Example one: The string ' {' A ': ' B ', ' B ":" \\\\ "}" passed to Json.parse, first the parser to extract the string enclosed in single quotes that the first \ escaped the second \ Third \ escaped fourth \, which means the actual output string is {"A": "B", "B" : "\"} (available via Console.log (' {"A '):" B "," B ":" \\\\ "}"), then formally converted to JS object when there is an escape, that is, the actual output character to the first \ Escape second \ (at this time only two \). So Console.log (Json.parse (' {"a"): "B", "B": "\\\\"}); the output is object {A: "B", B: "\"}, which means that the actual data displayed is a \ (actually output a \ description) )。
Example two:
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": "\\\\"}})); Output the following figure
Pic
According to the escape rule, the actual output of a \ Before this \ must have a \. So as the first line of output is written as ' {' A ': ' B "," B ":" \\\\ "," C ": {" B ":" \\\\ "," a ": {" B ":" \\\\ "}}", can be validated by the third output.
To summarize, if you want to appear in a JS object, you need to have four \ in the JSON string.

for other special characters
1. Double quotation marks ("), if the correct occurrence of double quotes should be \\\"
2.\n, if you want to appear the correct line-wrapping need to be in the JSON string is \\n, in fact, is the first \ \ Escape, n into the ordinary characters, in the resolution of the JS object N and before the \ (only one \) is interpreted as a newline. The following two are similar to this.
3.\r,\\r
4.\t,\\t
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.