如何使用node.js amqp模块将AQMP消息缓冲区转换为JSON对象?
我正在使用node.js amqp模块来读取队列中的消息。 以下是队列中有消息时调用的callback函数:
function onMessage(message, headers, deliveryInfo) { console.log(message); //This prints buffer //how to convert message (which I expect to be JSON) into a JSON object. //Also how to get the JSON string from the 'message' which seems to be a buffer }
谢谢。
如果您收到一个包含JSON的缓冲区,那么您需要将其转换为string以输出对控制台有意义的内容:
console.log(message.toString())
如果要将该string转换为完整的JavaScript对象,则只需parsingJSON:
var res = JSON.parse(message.toString())
编辑: node-amqp似乎能够直接发送JavaScript对象(请看这里 ),你不应该接收缓冲区,而是JavaScript对象…检查你如何发送消息。
message.data.toString()返回适当的JSONstring。