一. JSON对象和JSON字符串之间的转换
- json字符串----->json对象
- 使用JSON.parse()函数
var jsonStr = ‘{"name":"zhangsan","age":23,"email":"[email protected]"}‘; var json = JSON.parse(jsonStr); console.log(json);//输出:Object {name: "zhangsan", age: 23, email: "[email protected]"}
- 使用eval()函数
var jsonStr = ‘{"name":"zhangsan","age":23,"email":"[email protected]"}‘; var json = eval ("(" + jsonStr + ")"); console.log(json);//输出:Object {name: "zhangsan", age: 23, email: "[email protected]"}
- 使用jQuery插件jQuery.parseJSON()
var jsonStr = ‘{"name":"zhangsan","age":23,"email":"[email protected]"}‘; var json = jQuery.parseJSON(jsonStr); console.log(json);//输出:Object {name: "zhangsan", age: 23, email: "[email protected]"}
- 使用JSON.parse()函数
- json对象------>json字符串
- 使用JSON.stringify()
var json = {name: "zhangsan", age: 23, email: "[email protected]"}; var jsonStr = JSON.stringify(json); console.log(jsonStr);//输出:"{"name":"zhangsan","age":23,"email":"[email protected]"}"
- 把数据在前后端之间传递,需要把json对象转换为json字符串
原文地址:https://www.cnblogs.com/java-gcs/p/11166124.html
时间: 2024-10-11 04:42:07