O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required