uni-app中encodeURIComponent(JSON.stringify( 数组 ))怎样解析

uni-app中encodeURIComponent(JSON.stringify( 数组 ))怎样解析

示例代码:

webview传值 encodeURIComponent(JSON.stringify( 数组 )) 解析不了

操作步骤:

webview传值 encodeURIComponent(JSON.stringify( 数组 )) 解析不了

预期结果:

解析成功

实际结果:

Uncaught SyntaxError: Unexpected token , in JSON at position0

bug描述:

webview传值 encodeURIComponent(JSON.stringify( 数组 )) 解析不了

开发环境 版本号 项目创建方式
Windows win10教育版 1909 HBuilderX
Android Android 11
华为 18398794488
vue
云端
HBuilderX 3.1.19

更多关于uni-app中encodeURIComponent(JSON.stringify( 数组 ))怎样解析的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

let good={name:‘哈哈哈’}
// 编码
let data=encodeURIComponent(JSON.stringify(good))
console.log(data);
// 解析
let str=JSON.parse(decodeURIComponent(data));
console.log(str); JSON.parse(decodeURIComponent(data)); 这样写应该没问题

更多关于uni-app中encodeURIComponent(JSON.stringify( 数组 ))怎样解析的实战教程也可以访问 https://www.itying.com/category-93-b0.html


好像还是有问题喔

在uni-app中解析encodeURIComponent(JSON.stringify(数组))时出现JSON解析错误,通常是因为接收端没有正确进行双重解码。正确的解析步骤应该是:

  1. 先用decodeURIComponent()对接收到的字符串进行URL解码
  2. 再用JSON.parse()将解码后的字符串转为JavaScript对象

示例代码:

// 接收端处理
let receivedData = '从webview接收到的编码字符串';
let decodedString = decodeURIComponent(receivedData);
let originalArray = JSON.parse(decodedString);
console.log(originalArray); // 得到原始数组
回到顶部