JavaScript Json (GO)

Source: Internet
Author: User
Tags tojson

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)

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.