关于JSON.stringify的细节

JSON 的常规用途是同 web 服务器进行数据交换。
在向 web 服务器发送数据时,数据必须是字符串。
通过 JSON.stringify() 把 JavaScript 对象转换为字符串。

js会有一个自动拆箱功能,而stringify正好相反

let a1 ="1";
let a2 =1
let b1 ="true"
let b2 =true
let c ="str"

//默认会自动拆箱(布尔值不会)
console.log(a1+":"+b1+":"+c)//1:true:str
console.log(a2+":"+b2+":"+c)//1:true:str
console.log(a1==a2)//true
console.log(b1==b2)//false

// 布尔值、数字、字符串的包装对象在序列化过程中会自动转换成对应的原始值。
console.log(JSON.stringify(a1)+":"+JSON.stringify(b1)+":"+JSON.stringify(c))// "1":"true":"str"
console.log(JSON.stringify(a2)+":"+JSON.stringify(b2)+":"+JSON.stringify(c))//  1: true: "str"

console.log(JSON.stringify(a1)==JSON.stringify(a2))//false
console.log(JSON.stringify(b1)==JSON.stringify(b2))//false
发布了29 篇原创文章 · 获赞 30 · 访问量 2053

猜你喜欢

转载自blog.csdn.net/Android_Cob/article/details/105718096