js字符串转json格式与json对象转字符串
- json字符串----->json对象json对象------>json字符串
-
使用JSON.parse()函数
this.dataList = JSON.parse(dataList);
-
使用eval()函数
var jsonStr = '{"name":"zhangsan","age":23,"email":"chentging@aliyun.com"}'; var json = eval ("(" + jsonStr + ")"); console.log(json);//输出:Object {name: "zhangsan", age: 23, email: "chentging@aliyun.com"}
-
使用jQuery插件jQuery.parseJSON()
var jsonStr = '{"name":"zhangsan","age":23,"email":"chentging@aliyun.com"}'; var json = jQuery.parseJSON(jsonStr); console.log(json);//输出:Object {name: "zhangsan", age: 23, email: "chentging@aliyun.com"}
-
-
使用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"}"
- 把数据在前后端之间传递,需要把json对象转换为json字符串
本文来自博客园,作者:青石小巷,转载请注明原文链接:https://www.cnblogs.com/lgnblog/p/10681788.html