JSON.parse()
兼容性:Chrome,Firefox (Gecko) 3.5 (1.9.1),IE 8.0,Safari 4.0
JSON.parse('[1, 5, "false"]');
JSON.parse('{"p": 5}', function (k, v) {
if(k === '') return v; // 若是到了最頂層,則直接返回屬性值,
return v * 2; // 不然將屬性值變爲原來的 2 倍。
}); // { p: 10 }
JSON.stringify()
兼容性:Chrome,Firefox (Gecko) 3.5 (1.9.1),IE 8.0,Safari 4.0
var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
var json1 = JSON.stringify(student);
//{"name":"Lanny","age":"25","location":"China"}
//只保留:name及locaiton
var json2 = JSON.stringify(student,["name","location"]);
//{"name":"Lanny","location":"China"}
//處理特定屬性
var json3 = JSON.stringify(student, function (key, value) {
switch (key){
case "name":
return "my name is " + value;
default :
return value;
}
});
//{"name":"my name is Lanny","age":"25","location":"China"}
json