Json object
Json is a strict subset of Javascript that utilizes patterns in Javascript to represent structured data
Json can be passed directly to eval() without creating a DOM object
Json syntax
Json syntax can represent values of three data types
Simple values
The Numbers: 3
String: “Hello Word! Different from Javascript: double quotes must be used (syntax error)
Boolean values are the same as null, but are generally complex types for Json tables
object
As with Js literals, the difference is that attributes are quoted
There are no variables, no declarations of variables, etc
You don’t need a semicolon at the end
An array of
With the JS array literal form, the only difference is that there is no variable and semicolon, can also be nested with objects to form a very complex form
Json serialization
Json.stringfy(Object, filter, formatting options)
1. Filtering results
If the filter argument is an array: the result will contain only the listed properties
var book={
"title":"Javascript"."authors": ["NNNNn C.jjjj"]."edition":3."year":2001
}
var jsonText=JSON.stringify(book,["title"."edition"]);
console.log(jsonText);//{"title":"Javascript","edition":3}
Copy the code
If the second argument is a function, the function’s two arguments (key, value) —- normally return the corresponding key value. If the return value is undefined, the corresponding attribute is ignored
var book={
"title":"Javascript"."authors": ["NNNNn C.jjjj"]."edition":3."year":2001
}
var jsonText=JSON.stringify(book,function(key,value){
switch(key){
case "authors":return value.join(",");
case "year":return 2001;
case "edition":return undefined;
default:returnvalue; }});console.log(jsonText);
Copy the code
2. The format
The third parameter:
① Number: indent
② String: Indent in string format
3. The toJSON () method
Do a filter for stringfy()
ToJSON () can be made to return any value: if undefined is returned — it is embedded in another object, resulting in null
If it is a top-level object, the result is undefined
var book={
"title":"Javascript"."authors": ["NNNNn C.jjjj"]."edition":3."year":2001.toJSON:function(){// The object is serialized as a string instead of an object
return this.title; }}var jsonText=JSON.stringify(book);
console.log(jsonText);//"Javascript"
Copy the code
Serialization step
(1) The toJSON() method is called if it exists and can be used to get earth-valid values. Otherwise, the object itself is returned
(2) If a second argument is provided, apply the function filter. Pass in the value of step 1 of the function filter
(3) Serialize the value returned in the second step
(4) Perform formatting if formatting parameters are provided
Json parsing
JSON. Parse (object, return function)
If the return function returns undefined, the corresponding key is removed from the result. If another value is returned, that value is inserted into the result
var book={
"title":"Javascript"."authors": ["NNNNn C.jjjj"]."edition":3."year":2001.releaseDate:new Date(2020.11.1)// Programmatically valid string new attributes preserve Date objects
}
var jsonText=JSON.stringify(book);
var bookCopy=JSON.parse(jsonText,function(key,value){// Parse restore
if(key=="releaseDate") {// The reductor encounters the releaseDate key and creates a new Date object based on the corresponding key
return new Date(value);
}else{
returnvalue; }})console.log(bookCopy.releaseDate.getFullYear());//2020 Calls the getFullYear method based on the parse object
Copy the code