JSON.parse() und JSON.stringify()

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字符串

Je suppose que tu aimes

Origine blog.csdn.net/weixin_42234899/article/details/129255937
conseillé
Classement