JAvascript権威ガイド第20章JSON

2452 ワード

//20.1   
//JAVASCRIPT   JSON     。
//JSON                ("hello world")   ({"name":"maosi"})   (["a","b","c"])

//20.1.2 json  

var person = { "name": "Nicholas", "age": 29 };
person = {
    "name": "Nicholas",
    "age": 29,
    "school": {
        "name": "Merrimack College",
        "location": "North Andover,MA"
    }
}
//js   
var person = { name: "Nicholas", age: 29 };


//20.1.3   (js   json     )
var values = [25, 'hi', true];
//      
[
    {
        "title": "Professnional javascript",
        "authors": [
            "Nicholas C Zakas"
        ],
        edition: 3,
        year: 2011
    },
    {
        "title": "Professnional javascript",
        "authors": [
            "Nicholas C Zakas"
        ],
        edition: 2,
        year: 2009
    }
]

//20.2       

//   
var book = {
    title: "professional JavaScript",
    authors: ["Nicholas C Zakas"],
    edition: 3,
    year: 2011,
    releaseDate:new Date(2011,1,1);
};
var jsontext = JSON.stringify(book);

//20.2.2      

jsontext = JSON.stringify(book, ["title", "edition"]); //          (          )
//           
jsontext = JSON.stringify(book, function (key, value){
    switch (key) {
        case "authors":
            return value.JSON(',');
        case "year":
            return 5000;
        case "edition":
            return undefined; //      undefined               ,ignore
        default:
            return value;
    }
   
});

//JSON     ,      JSON            
jsontext = JSON.stringify(book,null,4); //                 

JSON.parse(jsontext,function(key,value){
     if(key =='releaseDate'){
         return new Date(value); //              
     }
     else{
         return value;
     }
});

//20.3