我有一个ArrayBuffer
对象,我需要能够将其转换为String
到JSON
,但是我无法从中获取[Int8Array]
的值该对象,即使它显然在那里。
我已经尝试了所有变体,但是它们都返回未定义
console.log(result);//Returns the array buffer
//Following methods all return undefined?
console.log(result["[[Int8Array]]"]);
console.log(result[[[Int8Array]]]);
console.log(result[[["Int8Array"]]]);
console.log(result[Int8Array]);
console.log(result["Int8Array"]);
如何获取对象中明显可用的所有Int8Array或UInt8Array值?
答案 0 :(得分:3)
您需要实例化一个ArrayBuffer
才能获取它们的值,您无法使用var buf = new ArrayBuffer(8);
var int8view = new Uint8Array(buf);
console.log(int8view)
实例直接访问它们。
{{1}}
JSFiddle:https://jsfiddle.net/v8m7pjqb/
答案 1 :(得分:0)
如果您想要一个字符串,则可以执行此操作而不必访问任何类型的数组:
var str = new TextDecoder().decode(arrayBuffer)
var json = JSON.parse(str)
如果您想直接使用json
var json = await new Response(arrayBuffer).json()