小程序使用json.parse出現的問題:
原因: JSON.parse無法識別某些url中的特殊字符,所以報錯
跳轉時用 encodeURIComponent()
wx.navigateTo({
url: '/pages/paymentDetail/paymentDetail?details=' + encodeURIComponent(nextDatas)
});
調轉頁 decodeURIComponent()
onLoad: function (options) {
var that = this
var newData= decodeURIComponent((options.details));
var payDetailList = JSON.parse(newData);
}
總結:
我們在JSON.stringify()之後將變量使用encodeURIComponent函數處理,這個encodeURIComponent() 函數可以把字符串作爲 URI 組件來進行編碼。在跳轉到目標頁面接收時用decodeURIComponent對URI 組件進行解碼,後面在通過JSON.parse()將變量還原,這樣子就能達到預期效果了。