ECMAscriptのグローバルオブジェクト----JSON

3965 ワード

JSON.stringify(),  value(Object,Array,String,Number...)    JSON   
    JSON.parse(),  JSON     js   
    toJSON(),   JSON.stringify      (     )   

   IE8+,FF3.5+,safari4+,opera10.5+,chrome

//
* ===================ECMAscript5     JSON=====================
* Json:            。  js syntax       、  、   、  、   、 null
* Note:ECMAScript5         JSON  ,
         JSON   (JSON.stringify()),
   JSON     js  (JSON.parse())。
* support: IE8+,FF3.5+,safari4+,opera10.5+,chrome
* IE6,7 : https://github.com/douglascrockford/JSON-js
//

//
* JSON.stringify()
* @specify : serialization(   )
* @method : JSON.stringify(value,filter,indent);
* @return : JSON   

* @param : value {type : String|Object|String|Number|Boolean|null} {explain :              }
* @param : filter : {type : []|{}} {explain :          ,       }
* @param : indent : {type : Number |     } {explain :               ,  10 ;            }
//
var gather = {
id : 1314,
name : 'pom',
infor : {
age : 20,
sex : 'man',
marry : false,
identity : 622421,
habit : ['  ','  ','   ','  ',true]
},
family : ['  ','  ','  '],
likeGames : ['PCgame','Netgame']

};
var jsonText = JSON.stringify(gather,null,4);

//        ,               
var jsonText1 = JSON.stringify(gather,['id','family'],'=');

var jsonText2 = JSON.stringify(gather,function(key,val){
switch(key){
case 'id' :
return 'id is ' + val;
case 'family' :
return val.join('@');
case 'infor' :
//infor val      JSON.stringify()
//return JSON.stringify(val,["age","sex"]);
return Object.prototype.toString.call(val).slice(8, -1);
case 'likeGames' :
//    undefined     
return undefined;
//   default,                     。
default :
return val;
}
},10);
// console.log(jsonText);
// console.log(jsonText1);
// console.log(jsonText2)

//
* toJSON()
* @specify : JSON.stringify()                    ,           toJSON()  
* @method : date.toJSON()
* @return :          。
* 
* JSON.parse() ,eval_r()          js      。 IE8           。
* @specify :  json         javascript 。
* @method : JSON.parse(val,replacer)

* @param : val{type : String} {explain :      json   }
* @param : replacer {type : function} 
{explain :  JSON.stringify()       ,  2   ,key,val,       json    }

//
var products = {
name : "leading",
"time" : new Date(2012,03,1),
toJSON : function(){
//   name
return this.name;
}
}
var proStr = JSON.stringify(products);
console.log(proStr);

//obj  relaeseDate            json 
var obj = {
title : '     ',
type : 'primitive' ,
releaseDate : new Date(2012,03,1)
};
//   json 
var o = JSON.stringify(obj);
console.log(o);

// parsedO      Date  (             Date  ,  parsedO.releaseDate        Date  )
var parsedO = JSON.parse(o,function(k,v){
if(k == 'releaseDate'){
return new Date(v);
}else{
return v;
}
});
console.log(parsedO);
//    getFullYear()
console.log(parsedO.releaseDate.getFullYear()); //2012

     :


 toJSON()   JSON.stringify      (     )  ,         。
          JSON.stringify()           :
      (1)     toJSON()              ,      。  ,          
      (2)           ,         ,            (1)     。
      (3)   (2)                。
      (4)           ,          。