gpt4 book ai didi

json - Node 流 : remote stream is returning JSON, 但在 ('data' 上)显示缓冲区

转载 作者:太空宇宙 更新时间:2023-11-03 23:09:19 25 4
gpt4 key购买 nike

我刚刚开始使用 Node stream s。

我的库的演示代码使用:

stream.pipe(process.stdout, {end: true});

效果很好,将 JSON block 打印到标准输出。

我想使用:

stream.on('data', function(chunk) {
console.log(chunk)
}

但是我得到了一个二进制缓冲区:

chunk! <Buffer 7b 22 73 74 72 65 61 6d 22 3a 22 20 2d 2d 2d 5c 75 30 30 33 65 20 35 35 32 38 38 36 39 62 30 30 33 37 5c 6e 22 7d>  

有没有办法可以使用 on('data') 并查看 JSON?

最佳答案

我相信您应该在流上运行 stream.setEncoding('utf8'),这样 Node.js 核心就会自动解码 utf8。

您可能像之前建议的那样使用chunk.toString('utf8'),因为它可能会在边界上混淆 unicode 字符,除非您确定数据将位于一个 block 中。

关于json - Node 流 : remote stream is returning JSON, 但在 ('data' 上)显示缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21785950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com