Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在nodejs中将缓冲区格式化为JSON?_Javascript_Python_Node.js_Python 3.x_Buffer - Fatal编程技术网

Javascript 如何在nodejs中将缓冲区格式化为JSON?

Javascript 如何在nodejs中将缓冲区格式化为JSON?,javascript,python,node.js,python-3.x,buffer,Javascript,Python,Node.js,Python 3.x,Buffer,我在Nodejs应用程序中从一个流中收到一条缓冲区消息。我如何将其解码为string/JSON sock.on("message", function(topic, message) { console.log( "received a message ", message ); }); 我曾尝试使用toString()将格式设置为utf8,然后尝试使用缓冲区npm模块,但找不到方法。我也搜索了类似的问题,但没有成功 当我用python尝试下面的示例代码时,我能够看

我在Nodejs应用程序中从一个流中收到一条缓冲区消息。我如何将其解码为string/JSON

sock.on("message", function(topic, message) {


  console.log(
    "received a message ",
    message
  );
});
我曾尝试使用toString()将格式设置为utf8,然后尝试使用缓冲区npm模块,但找不到方法。我也搜索了类似的问题,但没有成功

当我用python尝试下面的示例代码时,我能够看到XML格式的输出。但是如何在nodejs中实现同样的效果呢

python代码:

from gzip import GzipFile
from io import BytesIO
import zmq
import xmltodict
import pprint
import json

context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.connect("tcp location")
subscriber.setsockopt_string(zmq.SUBSCRIBE, "topicname")
while true:

   multipart = subscriber.recv_multipart()
   address = multipart[0]
   contents = b''.join(multipart[1:])
   contents = GzipFile('','r',0,BytesIO(contents)).read()
   contents = contents.decode('UTF-8').replace('\n', '').replace('\r', '')
   print(contents)


subscriber.close()
context.term()
格式化输出


所有那些
00
08
等。。。它不是字符串-数据应该是什么?@JaromandaX我在问题中添加了一些额外的信息。当我尝试使用python示例代码将数据流解码为XML时。基本上,数据是XML格式的,以后可以解析为JSON.oh。。。这是gzipped,这将是原始问题中有用的信息我确信有一个用于node的gzip库-你只需要找到它Hanks,我发现这个模块zlib很有用,但是当我使用数据时,数据不是xml格式的。我想我需要做更多的解析。如果可能的话,你能给我举一些例子吗<代码>常量x=zlib.Unzip(消息)否,从未在节点中使用过gzip库