作者:天涯小犁_640 | 来源:互联网 | 2022-11-09 12:42
我收到一个JSON作为缓冲区。我想将其解析为可读或JSON对象。
但是,尽管使用了所有技术(JSON.stringify()
,toString('utf8'
),我仍无法完成它。
这是我到目前为止所拥有的:
这就是它给我的:
如何将其转换为可读的内容?
1> Mark Meyer..:
Your code is working. The buffer you have is actually the string "[object Object]"
.
let b = Buffer.from('[object Object]', 'utf8')
console.log(JSON.stringify(b))
// {"type":"Buffer","data":[91,111,98,106,101,99,116,32,79,98,106,101,99,116,93]}
console.log(b.toString('utf8'))
// [Object object]
The problem you need to figure out is why is a buffer with that string being sent. It seems like the sender of the buffer needs to call stringify
or otherwise serialize the object before sending it. Then you can turn it back to a string with toString()
and use JSON.parse()
on the string.