javascript - 如何在 React 中将 arrayBuffer 转换为 JSON
问题描述
我正在使用 Axios,我得到arraybuffer
了回应。我想转换arraybuffer
为json
. 我真的很努力,但没有找到合适的解决方案。有人可以帮助我。
谢谢
axios.post(
`${config.apiPath}/api/generatepdf`,
{
onlyForPdf: 'pdf',
},
{
responseType: 'arraybuffer',
headers: {
Accept: 'application/pdf',
},
}
).then((res) => {
console.log('2@ res', JSON.parse(new TextDecoder().decode([res.data])))
}
在控制台中它返回未定义
解决方案
使用以下语法切换回 JSON。
let arrayBufferConverted = JSON.parse(
String.fromCharCode.apply(null, new Uint8Array(res.data))
)
推荐阅读
- angular - 错误 TS2339:“Promise”类型上不存在属性“finally”
' - angular - 如何在 Angular 6 中使用 Observables 而不是 document.querySelector
- java - 引起:org.springframework.data.redis.serializer.SerializationException:无法反序列化;嵌套异常是 org.springframework.core.seriali
- java - 将 blob 转换为字符串以创建 json 对象,然后将字符串更改回 blob
- android - 如何从链接url中提取内容并将其保存在android中的sqlite
- php - Codeigniter redirect() 之后有什么方法可以在相同的滚动位置加载页面
- sql - Teradata 15:解析字符串
- iis - 使用符号链接访问 wwwroot 之外的文件夹
- arduino - 无法使用 SPIFFS
- node.js - Mailgun.js 是否提供发送模板的可能性?