小程序报错:Unexpected end of JSON input的问题分析,是使用了json.parse时存在特殊字符

小程序使用json.parse出现的问题;
在这里插入图片描述
原因
JSON.parse无法识别某些url中的特殊字符,所以报错

payment.js中

gotoDetailPage: function (e) {
var that = this;
const expense_id = e.currentTarget.dataset.expenseId

var list = that.data.expense
let thisPur = list.filter(item => item.id == expense_id)[0];
var nextDatas = JSON.stringify(thisPur)
console.log("nextDatas>>>>>>>>", nextDatas)
wx.navigateTo({
  url: '/pages/paymentDetail/paymentDetail?details=' + encodeURIComponent(nextDatas)
});

},

跳转页:paymentDetail.js

onLoad: function (options) {
var that = this
console.log(“options>>>”, options)
var newData= decodeURIComponent((options.details));
var payDetailList = JSON.parse(newData);
}

解决方案

我们在JSON.stringify()之后将变量使用encodeURIComponent函数处理,这个encodeURIComponent() 函数可以把字符串作为 URI 组件来进行编码。在跳转到目标页面接收时用decodeURIComponent对URI 组件进行解码,后面在通过JSON.parse()将变量还原,这样子就能达到预期效果了。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
根据提供的引用内容来看,这个错误出现在使用`JSON.parse`函数,表示JSON输入的结尾意外地出现了。这通常发生在传递给`JSON.parse`的字符串不完整或格式不正确。在这种情况下,可能是因为通过`JSON.stringify`序列化对象出现了错误,导致传递给`JSON.parse`的字符串不完整。 为了解决这个问题,需要确保在序列化对象没有出错,并且在传递字符串给`JSON.parse`没有任何格式问题。请仔细检查代码,确保所有的对象都被正确地序列化为字符串,并且在传递给`JSON.parse`之前没有任何编码或解码问题。 同,还要确保传递给`JSON.parse`的字符串是完整的,没有任何缺失的部分。如果问题仍然存在,建议在代码中使用调试工具来跟踪变量的值,并检查传递给`JSON.parse`的字符串是否符合预期。 总结来说,`JSON.parse SyntaxError: Unexpected end of JSON input`错误通常是由于传递给`JSON.parse`的字符串不完整或格式不正确导致的。确保正确序列化对象为字符串,并在传递给`JSON.parse`之前检查字符串是否完整和格式正确,以解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uniapp报错:SyntaxError: Unexpected end of JSON input](https://blog.csdn.net/qq_53742811/article/details/127648107)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [JSON.parse报错 SyntaxError:Unexpected end of JSON input](https://blog.csdn.net/qq_51081319/article/details/125990801)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值