?1.接收頁面報錯
Error in onLoad hook: "SyntaxError: Unexpected token u in JSON at position 0"
Unexpected token u in JSON at position 0
? ? at JSON.parse (<anonymous>)
2.發送頁面 ,JSON.stringify(item) ,將對象轉換為 JSON 字符串
goinfo(item,type){if(type){uni.navigateTo({url: '/pages/user/details?item='+JSON.stringify(item)})}else{uni.navigateTo({url: '/pages/user/details'})}},
3.接收頁面 ,JSON.parse()可以將JSON字符串JSON.stringify轉為一個對象
onLoad(opt) {//對需要進行解析的參數進行條件限定if (opt.curParam == undefined || opt.curParam == 'undefined') {//這里填寫當其為 undefined 或 ‘undefined ’時你需要進行的操作} else {this.curParmas = JSON.parse(decodeURIComponent(opt.curParam));}},
結語:?
JSON.stringify()的作用是將?JavaScript 對象轉換為 JSON 字符串,而JSON.parse()可以將JSON字符串轉為一個對象。
深拷貝JSON.parse(JSON.stringify(obj)),只能處理JSON格式的數據,對于函數、undefined、Symbol等類型無法處理。
encodeURIComponent() 函數可把字符串作為 URI 組件進行編碼。
decodeURIComponent()可對encodeURIComponent()函數編碼的URI進行解碼。