20th Chapter
First, the grammar
The syntax of JSON can represent the following three types of values:
(1) Simple value
(2) Object
There are some differences between JSON objects and JavaScript literals. For example, in JavaScript, the preceding object literal can be written as follows:
var object = {
"Name": "Nicholas",
"Age": 29
} ;
JSON represents the above objects in the following way:
{
"Name": "Nicholas",
"Age": 29
}
The difference: First, there is no declaration of the variable, and secondly, there is no semicolon at the end. Finally, the properties of the object must be enclosed in double quotation marks.
(3) array
Here is the array literal in javascript:
var values = [+, "HI", true];
In JSON, you can use the same syntax to represent the same array:
[]25, "HI", true]
Similarly, there are no variables and semicolons in the JSON array.
Second, parsing and serialization
1.JSON objects
The early JSON parser was basically the eval () function using JavaScript. Because JSON is a subset of JavaScript syntax, the eval () function can parse, interpret, and return JavaScript objects and arrays.
There are two methods of JSON objects: Stringgy () and parse (). In the simplest case, these two methods are used to serialize JavaScript objects into JSON strings and to parse JSON strings into native JavaScript values.
2. Serialization options
Json.stringify () In addition to the JavaScript object to serialize, you can also receive two additional parameters, which are used to specify a different way to serialize the JavaScript object. The first parameter is a filter, which can be an array or a function, and the second argument is an option that indicates whether indentation is preserved in the JSON string.
(1) Filter results
If the filter parameter is an array, then the result of Json.stringify () will contain only the attributes listed in the array. Take a look at the following example:
var book = {
"title": "Professional JavaScript",
"Authors": [
"Nicholas C.zakas"],
Edition:3, year:2011
} ;
var jsontext = json.stringify (book, ["title", "edition"]);
The second argument to Json.stringify () is an array that contains two strings: "title" and "Editon". These two properties correspond to the attributes in the object that will be serialized, so only the two properties are included in the returned result string:
{"title": "Professional JavaScript", "edition": 3}
(2) String indentation
The third parameter of the Json.stringify () method is used to control the indentation and whitespace characters in the result. If this parameter is a numeric value, it represents the number of spaces indented at each level. For example, to indent 4 spaces at each level, you can write code like this:
var book = {
"title": "Professional JavaScript";
"Authors": [
"Nicholas C. Zakas "
] ,
Edition:3,
year:2011
} ;
var jsontext = json.stringify (Nook, NULL, 4);
(3) ToJSON () method
Sometimes, json.stringify () does not satisfy the need for custom serialization of certain objects. In these cases, you can return its own JSON data format by calling the Tojson () method on the object.
ToJSON () can be supplemented as a function filter, so it is important to understand the internal order of serialization. Suppose you pass an object into Json.stringify () and serialize the object in the following order:
The method is called if the Tojson () method is present and can be used to obtain a valid value. Otherwise, the serialization is performed in the default order.
If the second argument is provided, the function filter should be used. The value passed into the function filter is the value returned by step (1).
Each value returned by step (2) is serialized accordingly.
If a third argument is provided, the corresponding formatting is performed.
3. Resolution Options
The Json.parse () method can also receive another parameter, which is a function that will be called on each key-value pair. In order to distinguish the substitution function received by json.stringify (), this function is called a restore function.
JavaScript Advanced Programming: 20th Chapter