今天在开发微信小程序需求的时候运用JSON进行页面之间的传值,发现传的值在转化的时候意外停止并报错 “SyntaxError:Unexpected end of JSON input
”。然后我发现传的一个数据中含有标签等特殊字符,如下图:
那么我是如何完成最终转换的呢?老规矩,废话不多说,直接上代码!
数据发送方:
Redeem(item) {
let obj = {
id : item.id,
brandCode : item.brandCode,
commodityName : item.commodityName,
memberNum :item.memberNum,
totalsaleNum : item.totalsaleNum,
exchangeValIntegral : item.exchangeValIntegral,
couponvalue : item.couponvalue,
memo : item.memo
}
const params = JSON.stringify(obj)
uni.navigateTo({
url: './XXXXXXX?params=' + encodeURIComponent(params)
})
}
数据接收方:
onLoad(option) {
if(option){
let params = JSON.parse(decodeURIComponent(option.params))
console.log(params,"mark");
this.id = params.id
// 你要进行的操作
}
},
注意:
encodeURIComponent() 函数 可把字符串作为 URI 组件进行编码。
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ’ ( ) 。
decodeURIComponent() 函数 可对 encodeURIComponent() 函数编码的 URI 进行解码。