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

本文探讨了JSON作为web服务器数据交换的常用格式,详细解释了如何使用JSON.stringify()将JavaScript对象转换为字符串,以及这一过程中的数据类型处理,如自动拆箱和原始值转换。
106

被折叠的 条评论
为什么被折叠?



