Um die Quelle herauszufinden, gehen Sie bitte zu https://www.sojson.com/json/json_stringify.html
JSON.parse und JSON.stringify sind gegenseitige Konvertierungen. Die beiden Methoden können mehrere Parameter enthalten. In diesem Artikel wird nur ein Parameter gezeigt, der auch häufig im Entwicklungsprozess verwendet wird.
JSON.parse(): Konvertiert einen JSON-String in ein JSON-Objekt
JSON.stringify(): Konvertiert JSON-Objekte in JSON-Strings
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字符串
![](https://img-blog.csdnimg.cn/img_convert/938b5c99dc482909c2b091cc7c0d6416.png)