js对象和类型化数组互相转换的方法
// 对象转化为类型化数组
function jsonToTypedArray(obj) {
const jsonString = JSON.stringify(obj)
const encodedString = encodeURIComponent(jsonString)
let base64 = btoa(encodedString)
const encoder = new TextEncoder()
const uint8array = encoder.encode(base64)
return uint8array
}
// 类型化数组转化为json
function typedArrayToJSON(binary) {
let str = ''
for (const i of binary) {
const s = String.fromCharCode(i)
str += s
}
const json = decodeURIComponent(atob(str))
return json
}
标签:binary,const,数组,js,json,str,类型化
From: https://blog.csdn.net/weixin_43576495/article/details/144102153