JSON.stringify():将JS对象转换为JSON格式字符串。
JSON.parse():将JSON串转换为JS对象。
var student = new Object();
student.name = "san";student.age = "24";
student.location = "China";
var json = JSON.stringify(student);
console.log("JSON.stringify方法效果:" + json);
var obj = JSON.parse(json);
console.log("JSON.parse方法效果: obj.name:" + obj.name + ";obj.age:" + obj.age + ";obj.location:" + obj.location);
控制台打印效果:
JSON.stringify方法效果:{"name":"san","age":"24","location":"China"}
JSON.parse方法效果:obj.name:san;obj.age:24;obj.location:China