Json.stringify (), add value (Object,array,string,number ...) Serialized as a JSON string json.parse (), parsing the JSON data to the JS native value Tojson (), supplementing the second parameter in json.stringify (function filter)
Support Ie8+,ff3.5+,safari4+,opera10.5+,chrome
* ===================ECMASCRIPT5 Global Object json===================== * Json: Just a lightweight data format. Use a subset of Jssyntax to represent objects, arrays, strings, numeric values, Boolean values, and null * NOTE:ECMASCRIPT5 defines a native JSON object that serializes an object into a JSON string (Json.stringify ()). Or parse the JSON data into a JS object (Json.parse ()). * Support:ie8+,ff3.5+,safari4+,opera10.5+,chrome * IE6,7:HTTPS://GITHUB.COM/DOUGLASCROCKFORD/JSON-JS//
* Json.stringify () * @specify: Serialization (serialized) * @method: Json.stringify (value,filter,indent); * @return: JSON string
* @param: value {type:string| Object| String| number| Boolean|null} {Explain: Incoming types can be listed for these} * @param: Filter: {type: []|{}} {Explain: Filter can be an array or a function} * @param: indent: {type:number | special symbol} {explain: If it is a number, the number of white space characters, up to 10, can also be passed directly into the indented symbol}//VA R gather = {id:1314, name: ' Pom ', infor: {age:20, Sex: ' Man ', Marry:false, identity:622421, habit: [' basketball ', ' billiards ', ' table tennis ', ' game ', True]}, family: [' Mom ', ' Papa ', ' brother '], likegames: [' pcgame ', ' netgame ']
}; var jsontext = json.stringify (gather,null,4);
The second argument is an array that serializes only the names listed in the array, var jsonText1 =json.stringify (gather,[' id ', ' family '], ' = ');
var jsonText2 = json.stringify (gather,function (key,val) {switch (key) {case ' id ': Return ' id ' + val; case ' family ': R Eturn val.join (' @ '); Case ' infor '://infor Val can also use Json.stringify ()//return json.stringify (val,["age", "sex"]); Return Object.prototype.toString.call (val). Slice (8,-1); Case ' likegames '://Remove the property by returning undefined return undefined; Be sure to default so that the other values passed in can return to the serialized results normally. Default:return Val; }},10); Console.log (Jsontext); Console.log (JSONTEXT1); Console.log (JSONTEXT2)
* Tojson () * @specify: Json.stringify () does not satisfy the need for custom serialization of certain objects, which can be invoked on the object that Tojson () method * @method: Date.tojson () * @return: Returns any serialized value. * * Json.parse (), Eval_r () can also parse and return JS objects and arrays. But IE8 the following browsers can have security implications. * @specify: Parses JSON strings into native JavaScript values. * @method: Json.parse (Val,replacer)
* @param: Val{type:string} {explain: JSON string to be parsed} * @param: replacer {type:function} {explain: and json.stringify () Two parameters are similar, accept 2 parameters, Key,val, but function to restore the JSON string
var products = {name: ' Leading ', ' Time ': New Date (2012,03,1), tojson:function () {///only returns the name Return THIS.name}} V Ar prostr = json.stringify (products); Console.log (PROSTR);
The Relaesedate object in obj is serialized as a valid JSON string var obj = {title: ' object's caption ', type: ' Primitive ', releasedate:new Date (2012,03,1)}; Convert to JSON string var o = json.stringify (obj); Console.log (o);
Revert to a Date object in Parsedo (a new Date object is created based on the corresponding value, a Date object is saved in the result Parsedo.releasedate property) var Parsedo = Json.parse (o, function (k,v) {if (k = = ' ReleaseDate ') {return new Date (v);} else{return v}}); Console.log (Parsedo); You can call getFullYear () Console.log (ParsedO.releaseDate.getFullYear ()); 2012
Principle of execution:
tojson () as the second parameter in json.stringify (function filter) complements, it is important to understand the internal order. Suppose the order in which an object is passed into Json.stringify () is serialized as follows: (1) Call this method if there is a Tojson () method and can obtain a valid value from it. Otherwise, perform serialization in the default order (2) If the second argument is provided, the function filter is applied and the value of the Passed-in function filter is the value returned in step (1). (3) the corresponding serialization of each value returned in step (2). (4) If a third parameter is provided, the corresponding formatting operation is performed.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.