The main advantage of JSON is that the format is streamlined, compared to XML. Generally used as data transmission, the front and back end of the transmission.
Now popular browsers generally support two methods of JSON, serialization and deserialization.
1. Json.stringnify ()
2. Json.parse ()
The following example is a common use of these two methods.
Note that the two methods of JSON ignore undefined's project properties, such as the item with key test is ignored.
varA;varperson = { "FullName": "Mr Xi", "Wife": "Miss Peng", "Children": ["Xi1", "Xi2"], "Age": 53, "Test": A};console.log (a); //undefinedvarstr = json.stringify (person);//typically serialized, can be uploaded to the backgroundConsole.log (str);//{"FullName": "Mr Xi", "Wife": "Miss Peng", "Children": ["Xi1", "Xi2"], "age":varJobj = Json.parse (str);//after uploading to the foreground from the backstage, the analysisConsole.log (Jobj);
Most of the time, most of us neglect the second parameter of these two methods. The second parameter is primarily used for filtering (either an array, or a function).
When an array is passed, the keys for the JSON object are passed, and the property is included, otherwise filtered out.
When a function, the function has two parameters are key and value, for their own business processing, return undefined is not included, otherwise return value.
varSTR1 = json.stringify (person, ["FullName", "children"]); Console.log (str1);//{"FullName": "Mr Xi", "Children": ["Xi1", "Xi2"]}varstr2 = json.stringify (person,function(k,v) {if(k = = "Children")) {V.push ("Xi3"); returnv; } Else if(k = = "Age"){ returnundefined; } Else { returnv; }}); Console.log (str2) ;//{"FullName": "Mr Xi", "Wife": "Miss Peng", "Children": ["Xi1", "Xi2", "Xi3"]}
Use of JSON in JS