直接说重点吧,记录留存。
uni-app开发的小程序添加了扫码功能,代码如下:
// 点击扫码
bindScanTap() {
uni.scanCode({
success: (res) => {
try {
//判断二维码是否为队列二维码
let research = JSON.parse(res.result)
if (!(research.researchId && research.deptId && research.diseaseId)) {
util.showToast("解析失败")
return
}
} catch (e) {
util.showToast("解析失败")
return
}
},
fail: () => {
util.showToast("扫码失败")
}
})
}
扫码结果:
{
"charSet": "utf-8",
"result": "{"type":"4","researchId":22110,"deptName":"【测】xxx科","diseasesName":"体检1","projectName":"入组测试2","deptId":266,"diseaseId":221}",
"codeVersion": 12,
"errMsg": "scanCode:ok",
"rawData": "77u/eyJ0eXB...2LCJkaXNlYXNlSWQiOjIyMX0=",
"scanType": "QR_CODE"
}
安卓和iOS端App都没有问题,iOS端小程序也没有问题,小米手机扫码报错:
<SyntaxError: Unexpected token '', "{"type":""... is not valid JSON>
SyntaxError: Unexpected token '', "{"type":""... is not valid JSON
at JSON.parse (<anonymous>)
at Object.success (https://usr/app-service.js:3432:3766)
一开始以为是json
字符中"
转义的问题,于是给加上\"
,还是报错,又尝试了给字符串前后加上"
或'
。折腾许久还是不行。
最后咨询AI解决: