user1510580 user1510580 - 2 years ago 537
Javascript Question

JavaScript byte array to JSON and back

I am having trouble converting a JSON string back to byte array. My byte array was converted to JSON string through JSON.stringify(bytes). If I use JSON.parse to convert the string back to JS, I only get an object, not an array any more.
For example in the JS console:

> var bytes = new Int32Array([101, 102, 103]);
> var s = JSON.stringify(bytes);
> s;
> var a = JSON.parse(s);

> a;
Object {0: 101, 1: 102, 2: 103}

How can I get the original byte array back?

gp. gp.
Answer Source
var arr = []; 
for(var p in Object.getOwnPropertyNames(a)) {
    arr[p] = a[p];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download