An overview of the effect of the escape character (\) on json.parse in JavaScript

Source: Internet
Author: User
Tags eval json
JSON is a built-in object that provides a stringify and parse method for converting a JS object to a JSON-standard string, which converts a JSON-compliant string to a JS object, and this article introduces the effect of the escape character on the Json.parse method  

As explained in ECMA262 version fifth, 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 (it executes the code in the JSON string), and this article writes only JSON)
This article writes about the effect of the escape character on the Json.parse method. The

generally encounters two escapes when the Json.parse parameter contains the transfer character, but the first is the escape of the string itself, and the second is the escape from the real to the JS object. The

example is as follows:
Example one: To pass the string ' {' A ': ' B ', ' B ': '} ' to Json.parse, first the parser extracts the string enclosed in single quotes as the first escaping the second third escaping the fourth, which means that the actual output string is {"A ":" B "," B ":" "} (available via Console.log (' {" a ":") B "," B ":" "}"), and then formally converted to the JS object when there is an escape, that is, the actual output character to the first escape in the 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 one (the actual output is one note).
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 ":" "}}"); The output is the following figure
Pic
According to the escape rule, the actual output one must have been before this one. So as the first line of output is written as ' {' A ': ' B "," B ":" "," C ": {" B ":" "," a ": {" B ":" "}}}", can be validated by the third output.
Summary, if you want to appear in a JS object, you need to have four occurrences in the JSON string.

for other special characters
1. Double quotes ("), if the correct double quotation mark should be"
2.N, if the correct line break is required to be n in the JSON string, it is actually the first to escape from N, n becomes a normal character, and when parsed as a JS object, N and previous (only one) are interpreted as wrapping. The following two are similar to this.
3.r,r
4.t,t

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.