JSON is the abbreviation for JavaScript Object notation, which is a data interchange format.
Finally, on a day in 2002, Grass Crockford Douglas Crockford, a software engineer who had been fooled by a handful of giant software companies in order to save a deep abyss, invented the ultra-lightweight data Interchange format of JSON.
As a senior architect of Yahoo, Douglas has a natural fondness for JavaScript. The JSON he designed is actually a subset of JavaScript. In JSON, there are a total of several data types:
- Number: Exactly the same as JavaScript
number
;
- Boolean: is JavaScript
true
or false
;
- String: It's JavaScript
string
;
- Null: it is JavaScript
null
;
- Array: That's the
Array
way JavaScript is represented-- []
;
- Object: The way JavaScript is
{ ... }
represented
I've finally specified the origin of JSON.
Serialization of
var xiaoming = { name: ' Xiaoming ', age:14, gender:true, height:1.65, grade:null, ' Middle-school ' : ' \ ' w3c\ ' Middle School ', skills: [' JavaScript ', ' Java ', ' Python ', ' Lisp ']}; Json.stringify (xiaoming); ' {' name ': ' Xiaoming ', ' age ': +, ' gender ': true, ' height ': 1.65, ' grade ': null, ' middle-school ': ' \ ' w3c\ ' Middle school ', ' Skills ": [" JavaScript "," Java "," Python "," Lisp "]} '
We can also add some parameters to make it easier to view
Json.stringify (xiaoming, NULL, ' ); output { "name": "Xiaoming", "Age ": +, "gender": true, "height": 1.65, "grade": null, "Middle-school": "\" w3c\ "Middle School", "skills": [ "JavaScript", " Java ", " Python ", " Lisp " ]}
The second parameter is used to control how the object's key value is filtered, and if we only want to output the specified property, we can pass in Array:JSON.stringify (xiaoming, [' Name ', ' skills '], " ); Result: { " name ": "Xiaoming", "skills": [ "JavaScript", "Java", "Python", "Lisp" ]}
You can also pass in a function so that each key-value pair of an object will be processed first: function convert (key, value) { if (typeof value = = = ' String ') { return Value.touppercase (); } return value;} Json.stringify (xiaoming, convert, " ); The above code capitalizes all attribute values: { " name ":" Xiaoming "," Age ":" Gender ": True, "height": 1.65, "grade": null, "Middle-school": "\" w3c\ "Middle School", "skills": [ "JAVASCRIPT", "JAVA", "PYTHON", "LISP" ]}
If we also want to control exactly how to serialize xiaoming, you can define a Tojson () method for Xiaoming to return directly the data that the JSON should serialize: var xiaoming = { name: ' Xiaoming ', age:14, Gender:true, height:1.65, grade:null, ' middle-school ': ' \ ' w3c\ ' Middle school ', skills: [' JavaScript ', ' Java ', ' Python ', ' Lisp '], tojson:function () { return {///output only Name and age, and changed key: ' name ': this . Name, ' age ': This.age }; }; Json.stringify (xiaoming); ' {' Name ': ' xiaoming ', ' Age ': 14} '
Deserialization
To get a string in JSON format, we use Json.parse () to turn it into a JavaScript object: Json.parse (' [1,2,3,true] '); [1, 2, 3, True]json.parse (' {"name": "Xiaoming", "Age": 14} '); Object {name: ' Xiaoming ', Age:14}json.parse (' true '); Truejson.parse (' 123.45 '); 123.45json.parse () can also receive a function that transforms the parsed attribute: Json.parse (' {' name ': ' Xiaoming ', ' age ': ' + ', function (key, value) { // Put number * 2: if (key = = = ' name ') { return value + ' classmate '; } return value;}); Object {name: ' Xiao Ming classmate ', age:14} using JSON in JavaScript is that simple!
Source:
Http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/ 001434499490767fe5a0e31e17e44b69dcd1196f7ec6fc6000
JavaScript Json (GO)