5

I have an ArrayBuffer object that I need to be able to convert to String to JSON, but I can't get the value of the [Int8Array] out of the object, even though it is clearly there.

enter image description here

I've tried all variations on this, but they all return undefined

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

How can I get all the Int8Array or UInt8Array values that are clearly available in the object?

3
  • You should be able to get the individual values using console.log(result[0]) etc. Commented Jan 17, 2019 at 8:33
  • 1
    That did not work, also always returns undefined. The comment below is correct in that I needed to instantiate a new ArrayBuffer for the values. Commented Jan 17, 2019 at 8:37
  • stackoverflow.com/a/11555049/5734311 Commented Jan 17, 2019 at 8:39

2 Answers 2

9

You can use the textDecoder that accepts ArrayBuffer (as well as uint8array) without having to deal with Uint8array's:

var str = new TextDecoder().decode(arrayBuffer)
var json = JSON.parse(str)

if you want to get straight to json

var json = await new Response(arrayBuffer).json()
Sign up to request clarification or add additional context in comments.

Comments

8

You need to intiantiate a new Uint8Array to get their values, you can't access them directly using your ArrayBuffer instance.

var buf = new ArrayBuffer(8);
var int8view = new Uint8Array(buf);
console.log(int8view)

JSFiddle : https://jsfiddle.net/v8m7pjqb/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.