简体   繁体   English

如何将 arraybuffer 转换为 json?

[英]How can I convert arraybuffer to json?

I want to make a chrome-extension.我想做一个 chrome 扩展。 I need to get the request payload and fetch it.我需要获取请求有效负载并获取它。 I get the data for requesting like this:我得到这样请求的数据:

ArrayBuffer(439) {}
[[Int8Array]]: Int8Array(439) [123, 34, 99, 114, 97, 119, 108, 101, 114, 73, 110, 102, 111, 34, 58, 34, 48, 97, 110, 65, 102, 120, 110, 100, 103, 79, 71, 111, 89, 57, 69, 82, 71, 71, 115, 80, 104, 67, 68, 86, 50, 100, 114, 118, 116, 113, 102, 68, 86, 51, 76, 66, 113, 121, 102, 84, 69, 100, 105, 115, 115, 79, 80, 68, 120, 122, 81, 56, 95, 101, 106, 89, 89, 106, 68, 70, 110, 73, 65, 82, 117, 82, 99, 111, 119, 114, 67, 88, 114, 97, 109, 101, 45, 57, 69, 50, 111, 52, 109, 54, …]
[[Uint8Array]]: Uint8Array(439) [123, 34, 99, 114, 97, 119, 108, 101, 114, 73, 110, 102, 111, 34, 58, 34, 48, 97, 110, 65, 102, 120, 110, 100, 103, 79, 71, 111, 89, 57, 69, 82, 71, 71, 115, 80, 104, 67, 68, 86, 50, 100, 114, 118, 116, 113, 102, 68, 86, 51, 76, 66, 113, 121, 102, 84, 69, 100, 105, 115, 115, 79, 80, 68, 120, 122, 81, 56, 95, 101, 106, 89, 89, 106, 68, 70, 110, 73, 65, 82, 117, 82, 99, 111, 119, 114, 67, 88, 114, 97, 109, 101, 45, 57, 69, 50, 111, 52, 109, 54, …]
byteLength: (...)

ArrayBuffer(439) {}
[[Int8Array]]: Int8Array(439) [123, 34, 99, 114, 97, 119, 108, 101, 114, 73, 110, 102, 111, 34, 58, 34, 48, 97, 110, 65, 102, 120, 110, 100, 103, 79, 71, 111, 89, 57, 69, 82, 71, 71, 115, 80, 104, 67, 68, 86, 50, 100, 114, 118, 116, 113, 102, 68, 86, 51, 76, 66, 113, 121, 102, 84, 69, 100, 105, 115, 115, 79, 80, 68, 120, 122, 81, 56, 95, 101, 106, 89, 89, 106, 68, 70, 110, 73, 65, 82, 117, 82, 99, 111, 119, 114, 67, 88, 114, 97, 109, 101, 45, 57, 69, 50, 111, 52, 109, 54, …]
[[Uint8Array]]: Uint8Array(439) [123, 34, 99, 114, 97, 119, 108, 101, 114, 73, 110, 102, 111, 34, 58, 34, 48, 97, 110, 65, 102, 120, 110, 100, 103, 79, 71, 111, 89, 57, 69, 82, 71, 71, 115, 80, 104, 67, 68, 86, 50, 100, 114, 118, 116, 113, 102, 68, 86, 51, 76, 66, 113, 121, 102, 84, 69, 100, 105, 115, 115, 79, 80, 68, 120, 122, 81, 56, 95, 101, 106, 89, 89, 106, 68, 70, 110, 73, 65, 82, 117, 82, 99, 111, 119, 114, 67, 88, 114, 97, 109, 101, 45, 57, 69, 50, 111, 52, 109, 54, …]
byteLength: (...)

What I want is just like this:我想要的是这样的:

{"crawlerInfo":"0anAfxnUgyloq9TVwy6uXXuUPvEOtPJucqE4kaTfd010JVz_MuvWcojytMpfFvK0r4brEP0S6F3DRlnehTr9P0898aJtexul-uF0C5m3hOnyq7OEa0duEcDkFmAIwXcD2jYb3PlyOkzLdKKIESlCBOSn5LoEaRY9GZsFD1VPcsmwZPlEbgPxVNSfsUSzgG4Z26TCbHmAC0mou1CCKAm1UVT15UQX3tFp-tuYbGfT-k4C_cJGLxqZYh-SCARNN9ulH2lLkDjdV-p2wg-1fi3QijKwDXsSC_pfb_iKJPQY6QpBfz1WsayTmTUeCGyajiG3z_PpGvv3sdvzqO9Sr82yZ1T62QXefVCxZZkeqSLv6NIE95QD7ReFsiBfGsKyOkTwDkY1bAb-8Ei18uGlVbnKVicIgN66LORYLrZIj16"} {"crawlerInfo":"0anAfxnUgyloq9TVwy6uXXuUPvEOtPJucqE4kaTfd010JVz_MuvWcojytMpfFvK0r4brEP0S6F3DRlnehTr9P0898aJtexul-uF0C5m3hOnyq7OEa0duEcDkFmAIwXcD2jYb3PlyOkzLdKKIESlCBOSn5LoEaRY9GZsFD1VPcsmwZPlEbgPxVNSfsUSzgG4Z26TCbHmAC0mou1CCKAm1UVT15UQX3tFp-tuYbGfT-k4C_cJGLxqZYh-SCARNN9ulH2lLkDjdV-p2wg-1fi3QijKwDXsSC_pfb_iKJPQY6QpBfz1WsayTmTUeCGyajiG3z_PpGvv3sdvzqO9Sr82yZ1T62QXefVCxZZkeqSLv6NIE95QD7ReFsiBfGsKyOkTwDkY1bAb-8Ei18uGlVbnKVicIgN66LORYLrZIj16"}

As @wOxxOm said, convert it into a string and then parse it.正如@wOxxOm 所说,将其转换为字符串然后解析它。

Note: This will not work for IE (and possibly Edge as well).注意:这不适用于 IE(也可能适用于 Edge)。

 // Obviosuly replace this with whatever your array buffer is const arrayBuffer = new TextEncoder().encode(JSON.stringify({crawlerInfo: "crawler info"})) const json = JSON.parse(new TextDecoder().decode(arrayBuffer)) console.log(json)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM