1, grammar,
- JSON syntax supports three types of values
- Simple values: strings, values, Booleans, and null, undefined not
- Objects: Complex data types. Objects represent ordered key-value pairs. Each value can be a simple value or a complex type
- Array: Complex data type. An array can be an ordered list of values accessed by a numeric index. The value of an array can be any other type, including simple values, objects, and other arrays.
1. Simple values
- A number, a string, is valid JSON
- JSON strings must use double quotes
- Boolean values and NULL are also valid JSON values
2, objects,
- Objects in JSON must enclose property names in double quotes
{
"name": "xxx"."age": 19
}
Copy the code
- There are no variable declarations, no semicolons, and attributes must be quoted
3, arrays,
[25."hi".true]
Copy the code
2. Parsing and serialization
1. JSON objects
JSON.stringify()
- Serialize JavaScript to a JSON string
- Omit functions and prototype objects
- Any attribute with a value of undefined will be skipped
JSON.parse()
- Parse JSON to native JavaScript values
2. Serialization options
JSON.stringify()
You can also receive two parameters that specify other serialized JavaScript objects- Filters, which can be arrays or functions
- Option to use plain brocade result JSON string for better control of JSON serialization
1. Filtering results
- If the second argument is an array
JSON.stringify()
The returned result will contain only the properties of the objects listed in the array.
let book = {
title: "Professional JavaScript".authors: [
"Nicholas C. Zakas"."Matt Frisbie"].edition: 4.year: 2017
};
let jsonText = JSON.stringify(book, ["title"."edition"]);
// {"title":"Professional JavaScript","edition":4}
Copy the code
- If the second argument is a function, the behavior is different
- The supplied function takes two arguments: the attribute name key and the attribute value value.
- This key determines what action to perform on the corresponding property.
- Key is always a string, only an empty string if the value is not part of a key-value pair
- Returning undefined causes the property to be ignored
let book = {
title: "Professional JavaScript".authors: [
"Nicholas C. Zakas"."Matt Frisbie"].edition: 4.year: 2017
};
let jsonText = JSON.stringify(book, (key, value) = > {
switch (key) {
case "authors":
return value.join(",")
case "year":
return 5000;
case "edition":
return undefined;
default:
returnvalue; }});// {"title":"Professional JavaScript","authors":"Nicholas C. Zakas,Matt Frisbie","year":5000}
Copy the code
2. Indent the string
JSON.stringify()
The third argument controls indentation and whitespace- When the argument is numeric, it represents the number of Spaces indented at each level
- It wraps automatically.
- The maximum value is 10. If the value exceeds 10, the value is set to 10
- The argument is a string, which will be concatenated with this character
- The maximum value is 10, above which it is truncated
- When the argument is numeric, it represents the number of Spaces indented at each level
ToJSON () method
- In the to
JSON.stringify()
Add the method to the object, and serialization returns the appropriate JSON representation based on the method. JSON.stringify()
The steps of- If the actual value can be obtained
toJSON()
Method to get the actual value, otherwise use the default serialization - If a second argument is provided, the filter is applied, and the value passed to the filter function is the value previously returned
- The values returned from the previous step are serialized accordingly
- If a third argument is provided, indent it accordingly.
- If the actual value can be obtained
3. Parse options
-
Json.parse () can accept an additional argument, and this function is called once for each key-value pair
-
In contrast to the alternative filtering function passed to json.stringify (), this function is called the reductor
-
The function also takes two arguments, the property name and the property value, and also the return value
-
If the return function returns undefined, the corresponding key is removed from the result.
-
If any other value is returned, that value is inserted into the result as the value of the corresponding key.
-