添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

blob转json

blob 转换为 json 的方法:

1.使用 FileReader API 将 Blob 对象读取为 ArrayBuffer,然后将 ArrayBuffer 转换为字符串;

2.使用 fetch API 将 Blob 对象读取为 Response 对象,然后使用 response.json() 方法将其转换为 JSON。

以下是使用 FileReader API 的代码示例:

const reader = new FileReader();
reader.readAsArrayBuffer(blob);
reader.onload = function() {
  const arrayBuffer = reader.result;
  const json = JSON.parse(new TextDecoder().decode(arrayBuffer));
  console.log(json);

以下是使用 fetch API 的代码示例:

fetch(blob)
  .then(response => response.json())
  .then(json => console.log(json))
  .catch(error => console.error(error));