I am new to both Javascript and JSON worlds. I am wondering how I could convert an incoming Uint8Array data () to a JS object? Any help / pointers please. Here's what I have done as an experiment.
// arr is uint8Array incoming data
function myConvertFunc(arr) {
let str = "";
for (var i=0; i<arr.byteLength; i++) {
str += String.fromCharCode(arr[i]);
}
// Say, 'str' at this step looks like below :
/* {"type": "newEvent", "content": {"rec": [{"id1": "1", "event": "3A=","payload": "EZm9ydW0ub="}]}} */
var serializedData = JSON.stringify(str);
let message = JSON.parse(serializedData);
switch (message.type) {
case "newEvent":
log("In newEvent");
break;
.
.
.
default:
log("undefined message type");
}
}
Contrary to my understanding, the default case log : "undefined message type" is show in my logs. Could someone please help me figure out my mistake here?