Json,javascript Object NOTATION,JS is the most common form of structured data transmission at present.
JSON is not a programming language, but rather a data structure, like MP4, AVI, is just a format. (data structure can contain a lot of types)
Types of JSON values
- Simple values
- Object
- Array
Simple values: String, Number, Boolean, and null, note that undefined is not included.
Note: Strings in JSON must be in double quotation marks.
Objects: Objects are unordered key-value pairs, and values in key values can be simple values, objects, or arrays.
Note: The properties of objects in JSON must be enclosed in double quotes, and there is no concept of variables in JSON.
Array: A list of ordered values, and the value itself can be a simple value, an object, or an array.
Parsing and serialization
JS has a global object JSON, which has the stringify () and the Parse () method, respectively, to serialize JS and parse json.
var obj = {name: "Leo", Age:19,marry:false,other:undefined,none:null,};var J1 = json.stringify (obj); Console.log (J1); VA R J2 = Json.parse (J1); Console.log (J2)
Note: Because there is no undefined data type in JSON, the key value is ignored.
{"Name": "Leo", "Age": +, "marry":false, "none":null} falsenull}
Serialization options
The Json.stringify () function can also receive a second and a third parameter for richer functionality.
Filter
The second argument can be an array that contains the string form of an object's properties:
var obj = {name: "Leo", Age:19,marry:false,other:undefined,none:null,};var J1 = json.stringify (obj,["name", "Age",]); Co Nsole.log (J1); {"Name": "Leo", "Age": 19}
If the argument is a function:
var obj = {name: "Leo", Age:19,marry:false,other:undefined,none:null,};var J1 = json.stringify (obj,function (Key,value) {switch (key) {case ' age ': Return 20;case "None": return "no"; case "marry": Return "false";d Efault:return value;}); Console.log (J1); {"Name": "Leo", "Age": "Marry": "false", "none": "No"}
Indent in
If you want to indent 4 spaces:
var obj = {name: "Leo", Age:19,marry:false,other:undefined,none:null,};var J1 = json.stringify (obj,null,4); console.log (J1);
{ "name": "Leo", "age": +, false, null }
Note: The maximum indentation is 10 cells.
Special characters:
var obj = {name: "Leo", Age:19,marry:false,other:undefined,none:null,};var J1 = json.stringify (Obj,null, "--"); cons Ole.log (J1);
{--"name": "Leo"--"age":+,false,null}
Using Tojson:
var obj = {name: "Leo", Age:19,marry:false,other:undefined,none:null,tojson:function () {return this.name;}}; var J1 = json.stringify (obj); Console.log (J1); "Leo"
The order in which the objects are serialized is as follows
(1) if the ToJSON () method is present and can be used to obtain a valid value, the method is called. Otherwise, the object itself is returned. (2) If the second parameter is provided, apply this function filter. The value passed into the function filter is thevalue returned by step (1). (3) thecorresponding serialization of each value returned by step (2). (4) If a third parameter is provided, the corresponding formatting is performed.
Parsing options
the Json.parse () method can also receive another parameter, which is a function that will be called on each key-value pair .
var obj = {name: "Leo", Age:19,marry:false,other:undefined,none:null,releasedate:new Date (1),};var J1 = json.stringify (obj); Console.log (J1); var j2 = Json.parse (j1,function (key,value) {if (key = = "ReleaseDate") {return new Date (value);} else {return value;}}); Console.log (J2); Console.log (J2.releaseDate.getFullYear ());
{"name": "Leo", "Age": +, "marry":false, "none":null, "ReleaseDate": "2011-11-30t16:00:00.000z "}falsenull, Releasedate:thu Dec 00:00:00 gmt+0800 (China Standard Time)}age:19marry:falsename: "Leo" None:nullreleaseDate:Thu Dec 00:00:00 gmt+0800 (China Standard Time) {}__proto__: Object11
The Restore function encountered"ReleaseDate"Key, a new one is created based on the corresponding value.Date object. The result is
bookcopy.releasedate A Date object is saved in the property . Because of this, it is possible to invoke this object based on
getFullYear () method.
JS Elevation 3:json