json对象和json字符串之间的转化
一、json字符串----->json对象
1、使用JSON.parse()函数
var jsonStr = '{"name":"zhangsan","age":23,"email":"chentging@aliyun.com"}';
varjson =JSON.parse(jsonStr);
console.log(json);//输出:Object {name: "zhangsan", age: 23, email: "chentging@aliyun.com"}
2、使用eval()函数
varjsonStr = '{"name":"zhangsan","age":23,"email":"chentging@aliyun.com"}';
varjson = eval ("("+ jsonStr +")");
console.log(json);//输出:Object {name: "zhangsan", age: 23, email: "chentging@aliyun.com"}
3、使用jQuery插件jQuery.parseJSON()
varjsonStr = '{"name":"zhangsan","age":23,"email":"chentging@aliyun.com"}';
varjson = jQuery.parseJSON(jsonStr);
console.log(json);//输出:Object {name: "zhangsan", age: 23, email: "chentging@aliyun.com"}
二、json对象------>json字符串
使用JSON.stringify()
var json = {name:"zhangsan",age:23,email:"chentging@aliyun.com"};
var jsonStr = JSON.stringify(json);
console.log(jsonStr);//输出:"{"name":"zhangsan","age":23,"email":"chentging@aliyun.com"}"