admin管理员组文章数量:1290978
I am new to both Javascript and JSON worlds. I am wondering how I could convert an ining Uint8Array data () to a JS object? Any help / pointers please. Here's what I have done as an experiment.
// arr is uint8Array ining 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?
I am new to both Javascript and JSON worlds. I am wondering how I could convert an ining Uint8Array data () to a JS object? Any help / pointers please. Here's what I have done as an experiment.
// arr is uint8Array ining 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?
Share Improve this question asked May 24, 2013 at 0:01 SiddarthaSiddartha 1311 gold badge1 silver badge4 bronze badges 4- 3 What is a "Uint8Array"? – Matt Ball Commented May 24, 2013 at 0:02
- The Uint8Array type represents an array of 8-bit unsigned integers. Not sure if this is the answer you are looking for – Siddartha Commented May 24, 2013 at 0:04
- @MattBall: It's a typed array. See developer.mozilla/en-US/docs/JavaScript/Typed_arrays/… – Bergi Commented May 24, 2013 at 0:13
- 1 @Siddartha: Why do you need to put a JSON string into a typed array (or the other way round)? – Bergi Commented May 24, 2013 at 0:14
1 Answer
Reset to default 5var serializedData = JSON.stringify(str); let message = JSON.parse(serializedData);
That means if there are no errors that str === serializedData
(or at least two equal-looking objects).
Say, 'str' at this step looks like below:
{"type": "newEvent", "content": {"rec": [{"id1": "1", "event": "3A=","payload": "EZm9ydW0ub="}]}}
Now, if str
is the JSON string then you just want
var message = JSON.parse(str);
Currently, you did JSON-encode and then -decode the JSON string, with the result that message
was the string again and its type
property was undefined
.
本文标签: jsonjavascript How do I convert Uint8Array data to a JS objectStack Overflow
版权声明:本文标题:json - javascript: How do I convert Uint8Array data to a JS object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741501520a2382077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论