JSON.parse() と JSON.stringify()

ソースを確認するには、https://www.sojson.com/json/json_stringify.htmlにアクセスしてください。
JSON.parse と JSON.stringify は相互変換です。2 つのメソッドには複数のパラメーターを指定できます。この記事では、開発プロセスでもよく使用される 1 つのパラメーターのみを示します。

JSON.parse(): json文字列をjsonオブジェクトに変換します

JSON.stringify(): json オブジェクトを json 文字列に変換します

console.log(localStorage.getItem('userInfo')) // 得到的是JSON字符串
console.log(localStorage.getItem('userInfo').userName) // JSON字符串是string类型,所以不存在key-value,因此报undefined
console.log(JSON.parse(localStorage.getItem('userInfo'))) // JSON.parse将JSON字符串转化为JSON对象
console.log(JSON.parse(localStorage.getItem('userInfo')).userName) // 对象存在key-value值
console.log(JSON.stringify(JSON.parse(localStorage.getItem('userInfo')))) // JSON.stringify是将json对象转化为JSON字符串

おすすめ

転載: blog.csdn.net/weixin_42234899/article/details/129255937