JS基础之JSON对象
目录
- JS基础之JSON对象
- 对象转JSON字符串
- JSON转JS对象
对象转JSON字符串
JSON.stringify(value,replacer,space)
value
:要转换的JS对象
replacer
:(可选)用于过滤和转换结果的函数或数组
space
:(可选)指定缩进量
// 创建JS对象
let date = {
name:"张三",
age:19,
hobbies:["run","read"]
}
console.log(date)
// 将对象转换成JSON格式
let json_date = JSON.stringify(date,null,4)
console.log(json_date)
JSON转JS对象
JSON.parse()
参数为json对象
let date = {
name:"张三",
age:19,
hobbies:["run","read"]
}
console.log(date)
let json_date = JSON.stringify(date,null,4)
console.log(JSON.parse(json_date))
console.log(typeof (JSON.parse(json_date)))
le.log(typeof (JSON.parse(json_date)))
![image-20240221203920603](https://img-blog.csdnimg.cn/img_convert/c047168b98ed0d38a3a203c34d14f01a.png)