【发布时间】:2018-12-02 13:31:20
【问题描述】:
我目前正在使用 MQTT 发布/订阅,并且每当主题发布时都会收到一条消息,并且我收到的消息可能会因主题而异。
收到的 JSON 分配给消息
client.on('message', function (topic, message)
{ cmd: 'set', reqid: 'string', objects: [ { type: 'devices', data: ["tring"], execution: {"command": "OnOff", "params": { "on": true }} } ] }
我想将 JSON 保存在一个变量中并从中访问对象。
JSON 是字符串,当我尝试访问它的对象时,例如 (message.cmd),它会显示“未定义”
【问题讨论】:
-
你需要使用 JSON.parse()
-
如果您使用 json.parser.online.fr 中的 json 字符串在线检查 @anush。你知道这是你用括号的功能问题
-
更新你的函数:-client.on('message', function (topic, message) { {cmd: 'set', reqid: 'string', objects: [ { type: 'devices' , 数据: ["tring"], 执行: {"command": "OnOff", "params": { "on": true }} } ] } }
标签: javascript node.js mqtt