Using a DataView to access an ArrayBuffer

Sometimes, you don't want to work with JSON at all, but instead with pure binary data. JavaScript provides the DataView abstraction, which lets you perform typed accesses on an array buffer of memory, such as one obtained from an XMLHttpRequest object.

Getting ready

To begin, you need your data in an ArrayBuffer, such as the one returned by the XMLHttpRequest object. With this, you can create a DataView, and then using that DataArray, create a typed array over the data view to extract just the bytes that you're interested in. Let's see an example.

How to do it…

Here's a simple example:

var req = new XMLHttpRequest(); req.open("GET", url, true); req.responseType = "arraybuffer"; req.onreadystatechange = function ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.