ECMAScript 5 pairs of rows parsing JSON for specification, the global object JSON is defined. Browsers that support this object are IE 8+, Firefox 3.5+, Safari 4+, Chrome
and the Opera 10.5+.
There are two methods of JSON objects: Stringify () and Parse ()
Use the following:
Json.stringify () usage/parameter Introduction
1, json.stringify (jsonobj), jsonobj as JSON data, the method is to serialize the data in JSON format into a string type
var bigweb= {name: ' ZDW ' ,sex: ' man ' ,birth: ' 1989 ' }; var jsontext = json.stringify (Bigweb); Console.log (jsontext) // Jsontext output: "{ "Name": "Zdw", "Sex": "Man", "Birth": "1989"} "
2, Json.stringify (Jsonobj,arr), arr can be customized to output properties such as [' name '], refer to code 1below, or a method as follows Code Listing 2:
Code Listing 2: Firefox 3.5 and 3.6 for the implementation of Json.stringify () has a bug, when the function as the second parameter of the method , the bug will appear, that the function can only be used as a filter: return undefined means to skip a property , and any value returned to it will contain the corresponding attribute in the result. Firefox 4 Fixes this bug.
Code 1
var bigweb= {
name:' zdw ', Sex:' man ', Birth:' 1989 '}; var jsontext = json.stringify (bigweb,[' name '); Console.log (jsontext)//jsontext output: " {"Name": "Zdw"} "
//Code 2varbigweb={name:' Zdw ', Sex:' Man ', Birth:' 1989 '};varJsontext = json.stringify (book,function(key, value) {Switch(key) { Case"Name":returnvalue; Case"Sex":return5000; Case"Birth":returnundefined; //Return undefined the property will be filtered when the output isdefault:returnvalue;}});//output: "{" "Name": "Zdw", "Sex": "Man", "Birth": "1989"} "
3, the third parameter of the json.stringify (Jsonobj,arr,num|string) method,num (greater than 10, 10 processing) , is used for numeric types to control indents and whitespace in the result, using the following Code 1, if it is a custom string, use the following code 2:
Code 1
var bigweb= {name:' zdw ', Sex:' man ', Birth:' 1989 '}; var jsontext = json.stringify (bigweb,[' name '],4); // look at the output and look at the results .
// Code 2 var bigweb= {name:' zdw ', Sex:' man ', Birth:' 1989 '}; Json.stringify (bigweb,null, '--'); // the output is as follows:"{--" name ":" ZDW ",--" sex ":" Man ",--" Birth ":" 1989 "}"
Json.parse () usage/parameter Introduction
1, Json.parse (jsontext),Jsontext is a JSON-formatted string, the method is a JSON-formatted string, serialized into JSON data, in contrast to the json.stringify (), Use the following code:
var bigweb= {name:' zdw ', Sex:' man ', Birth:' 1989 '}; var jsontext ="Zdw", Sex: "Man", Birth: "1989"}
1, Json.parse (Jsontext,fn),fn This parameter is a function that will be called on each key-value pair, and if the Restore function returns undefined, the corresponding key is to be removed from the result, and if another value is returned, The value is inserted into the result with the following code:
varbigweb={name:' Zdw ', Sex:' Man ', Birth:' 1989 '};varJsontext =json.stringify (bigweb);varJsondata = Json.parse (Jsontext,function(key,value) {if(key = = ' name ') {returnvalue; }Else if(key== ' Birth ') {return1999; }Else if(Key = = ' Sex ') {returnundefined; }Else{//This else must be added ... Otherwise the result of the output is undefined returnvalue; }})//result of output: {name: "Zdw", birth:1999}
Parsing of JSON objects, usage of json.stringify ()/json.parse ()