标签:style blog http color java os strong io
一、JSON的语法可以表示以下三种类型的值:
1 {
2 "name":"蔡斌",
3 "age":21,
4 "isRich":false,
5 "school":{
6 "name":"广东工业大学",
7 "location":"广州市大学城"
8 }
9 }
1 [23,"i",false];
或
1 [ 2 { 3 "name":"蔡斌" 4 }, 5 { 6 "name":"张三" 7 } 8 ]
1 var person = { 2 name:"蔡斌", 3 age:21, 4 school:“广东工业大学” 5 } 6 7 var personJson = JSON.stringify(person);
1 {"name":"蔡斌","age":21,"school":"广东工业大学"} 2 //typeof personJson; 3 //String
1 var person = { 2 name:"蔡斌", 3 age:21, 4 school:“广东工业大学”. 5 phone:undefined, 6 sayHi:function(){ 7 alert("hi, i am caibin"); 8 } 9 }
1 console.log(JSON.stringify(person));
1 {"name":"蔡斌","age":21,"school":"广东工业大学"}
1 var caibinsname = JSON.stringify(person,["name"]);
1 var caibin = JSON.stringify(person,function(key,value){ 2 switch(key){ 3 case "name": 4 return "caibin"; 5 case "age": 6 return value + 1; 7 case "school": 8 return value; 9 default: 10 return value; 11 } 12 });
var caibinsname = JSON.stringify(person,["name","age"],4);
1 { 2 "name": "蔡斌", 3 "age": 21 4 }
再如:
1 var caibinsname = JSON.stringify(person,["name","age"],"**");
1 { 2 **"name": "蔡斌", 3 **"age": 21 4 }
1 var newDate = new Date(); 2 JSON.stringify(newDate);
1 var book = { 2 title:"JavaScript权威指南", 3 edition:6, 4 toJSON:function(){ 5 return "《"+this.title + " 第六版》" 6 } 7 }; 8 JSON.stringify(book);
1 var caibin = JSON.parse(personjson);
1 var caibin = JSON.parse(personjson,function(key,value){ 2 if(key == "name"){ 3 return "caibin"; 4 } 5 return value; 6 });
1 var book = { 2 "title":"JavaScript高级程序设计", 3 edition:3, 4 releaseDate:new Date(2011,11,1) 5 } 6 var bookJson = JSON.stringify(book); 7 8 var bookcopy = JSON.parse(bookJson,function(key,value){ 9 if(key == "releaseDate"){ 10 return new Date(value); 11 } 12 return value; 13 });
小谈一下JavaScript中的JSON,布布扣,bubuko.com
标签:style blog http color java os strong io
原文地址:http://www.cnblogs.com/iceseal/p/3876829.html