相关文章推荐
鼻子大的生姜  ·  Qt -- ...·  2 年前    · 
帅气的匕首  ·  vue 获取对象 键名_vue ...·  2 年前    · 

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));