如何在 Node-RED 函数中解析出 JSON
How Can I parse out JSON in a Node-RED Function
我在 Bluemix for IoT 中使用 Node-RED。
如何解析功能节点中的各个信息片段(如 cmdmsg 和 tempr),以便我可以在流中的其他节点中使用它?
我在尝试时遇到错误(见下文)
当我将 "debug" 节点设置为查看完整消息对象时,我正在接收 JSON 完整消息对象(来自节点中的物联网)。请参阅下面的对象。
在我看来 JSON 的格式正确。
我尝试将以下内容放入函数节点中,但我收到一条错误消息 "TypeError: Cannot read property 'tempr' of undefined"
函数参数如下:
return {有效载荷:msg.payload.d.tempr};
这里是消息对象
{
"topic": "iot-2/type/Arduino-tempsensor/id/FFFFFFFFFFFF/evt/status/fmt/json",
"payload": "{\n\"d\": {\n\"myName\": \"Arduino CF\",\n\"cmdmsg\": \"Weekly\",\n\"tempr\": -3,\n}\n}",
"deviceId": "FFFFFFFFFFFF",
"deviceType": "Arduino-tempsensor",
"eventType": "status",
"format": "json",
"_msgid": "ffffffff.55555"
}
注意:我混淆了设备 ID(mac 地址)和 msgid
关于如何解析数据以及我为什么会收到错误的任何想法?
抱歉,您的 JSON 有效载荷完全混乱,它应该如下所示:{"d": {"myName": "Arduino CF","cmdmsg": "Weekly","tempr": -3}}
您不应该在有效负载中看到任何 \
或 \n
,它们看起来像是来自客户端的转义字符。我也相信 tempr 值后的最后一个逗号不应该存在有效 JSON.
我不是 Arduino 专家,但我已经用 Raspberry Pi 和 Mosquitto 客户端进行了试验,这就是我成功向 IoTF 发送事件的方法:
mosquitto_pub -h <org>.messaging.internetofthings.ibmcloud.com -p 1883 -u "use-token-auth" -P "<token>" -i d:<org>:raspi:raspi2 -t iot-2/evt/message/fmt/json -m {"d":{"text":"Hello World"}}
如果工资单是正确的JSON你的声明return {payload:msg.payload.d.tempr};
将有效。
你看过这个吗:http://www.ibm.com/developerworks/cloud/library/cl-bluemix-arduino-iot2/
您显示的 JSON 字符串应该在您尝试访问其字段之前转换为 Javascript 对象。要做到这一点就像将 arduino 输出连接到 "JSON" 节点一样简单,它会为您进行转换(或者如果字符串 not 有效 JSON).
如果您想查看 msg 对象的结构,请将 JSON 节点的输出连接到调试节点。例如,如果您只是想用温度值替换 msg.payload,您也可以将它连接到 "change" 节点。您不需要在函数节点中使用任何自定义 javascript 代码来进行这样的简单更改。
这是您可以导入的示例流程...通过将有效负载粘贴到 "template" 节点来模拟 arduino 输出字符串:
[{"id":"1a79abfe.b8abb4","type":"inject","z":"58c8eb7a.5496c4","name":"send output","topic":"iot-2/type/Arduino-tempsensor/id/FFFFFFFFFFFF/evt/status/fmt/json","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":2740,"wires":[["9fc678fb.ae18e8"]]},{"id":"69e91778.e0c6e8","type":"json","z":"58c8eb7a.5496c4","name":"","property":"payload","action":"","pretty":false,"x":390,"y":2800,"wires":[["d066800f.60a9b","cf991eb1.f2a1a"]]},{"id":"9d8d7da2.2a7da","type":"debug","z":"58c8eb7a.5496c4","name":"msg string","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":2740,"wires":[]},{"id":"9fc678fb.ae18e8","type":"template","z":"58c8eb7a.5496c4","name":"arduino string","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"{\n \"d\": {\n \"myName\": \"Arduino CF\",\n \"cmdmsg\": \"Weekly\",\n \"tempr\": -3\n }\n}","output":"str","x":360,"y":2740,"wires":[["69e91778.e0c6e8","9d8d7da2.2a7da"]]},{"id":"d066800f.60a9b","type":"change","z":"58c8eb7a.5496c4","name":"extract tempr","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.d.tempr","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":2860,"wires":[["af730d72.2995a"]]},{"id":"af730d72.2995a","type":"debug","z":"58c8eb7a.5496c4","name":"tempr","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":630,"y":2860,"wires":[]},{"id":"cf991eb1.f2a1a","type":"debug","z":"58c8eb7a.5496c4","name":"msg object","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":2800,"wires":[]}]
如前所述,您的原始 JSON 字符串无效,因为尾随逗号 -- 但 可以 double-quotes 并且换行,只要它们用“\”转义。解析字符串时,无论如何都会将它们剥离。
我在 Bluemix for IoT 中使用 Node-RED。
如何解析功能节点中的各个信息片段(如 cmdmsg 和 tempr),以便我可以在流中的其他节点中使用它? 我在尝试时遇到错误(见下文)
当我将 "debug" 节点设置为查看完整消息对象时,我正在接收 JSON 完整消息对象(来自节点中的物联网)。请参阅下面的对象。
在我看来 JSON 的格式正确。
我尝试将以下内容放入函数节点中,但我收到一条错误消息 "TypeError: Cannot read property 'tempr' of undefined"
函数参数如下: return {有效载荷:msg.payload.d.tempr};
这里是消息对象
{
"topic": "iot-2/type/Arduino-tempsensor/id/FFFFFFFFFFFF/evt/status/fmt/json",
"payload": "{\n\"d\": {\n\"myName\": \"Arduino CF\",\n\"cmdmsg\": \"Weekly\",\n\"tempr\": -3,\n}\n}",
"deviceId": "FFFFFFFFFFFF",
"deviceType": "Arduino-tempsensor",
"eventType": "status",
"format": "json",
"_msgid": "ffffffff.55555"
}
注意:我混淆了设备 ID(mac 地址)和 msgid
关于如何解析数据以及我为什么会收到错误的任何想法?
抱歉,您的 JSON 有效载荷完全混乱,它应该如下所示:{"d": {"myName": "Arduino CF","cmdmsg": "Weekly","tempr": -3}}
您不应该在有效负载中看到任何 \
或 \n
,它们看起来像是来自客户端的转义字符。我也相信 tempr 值后的最后一个逗号不应该存在有效 JSON.
我不是 Arduino 专家,但我已经用 Raspberry Pi 和 Mosquitto 客户端进行了试验,这就是我成功向 IoTF 发送事件的方法:
mosquitto_pub -h <org>.messaging.internetofthings.ibmcloud.com -p 1883 -u "use-token-auth" -P "<token>" -i d:<org>:raspi:raspi2 -t iot-2/evt/message/fmt/json -m {"d":{"text":"Hello World"}}
如果工资单是正确的JSON你的声明return {payload:msg.payload.d.tempr};
将有效。
你看过这个吗:http://www.ibm.com/developerworks/cloud/library/cl-bluemix-arduino-iot2/
您显示的 JSON 字符串应该在您尝试访问其字段之前转换为 Javascript 对象。要做到这一点就像将 arduino 输出连接到 "JSON" 节点一样简单,它会为您进行转换(或者如果字符串 not 有效 JSON).
如果您想查看 msg 对象的结构,请将 JSON 节点的输出连接到调试节点。例如,如果您只是想用温度值替换 msg.payload,您也可以将它连接到 "change" 节点。您不需要在函数节点中使用任何自定义 javascript 代码来进行这样的简单更改。
这是您可以导入的示例流程...通过将有效负载粘贴到 "template" 节点来模拟 arduino 输出字符串:
[{"id":"1a79abfe.b8abb4","type":"inject","z":"58c8eb7a.5496c4","name":"send output","topic":"iot-2/type/Arduino-tempsensor/id/FFFFFFFFFFFF/evt/status/fmt/json","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":2740,"wires":[["9fc678fb.ae18e8"]]},{"id":"69e91778.e0c6e8","type":"json","z":"58c8eb7a.5496c4","name":"","property":"payload","action":"","pretty":false,"x":390,"y":2800,"wires":[["d066800f.60a9b","cf991eb1.f2a1a"]]},{"id":"9d8d7da2.2a7da","type":"debug","z":"58c8eb7a.5496c4","name":"msg string","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":2740,"wires":[]},{"id":"9fc678fb.ae18e8","type":"template","z":"58c8eb7a.5496c4","name":"arduino string","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"{\n \"d\": {\n \"myName\": \"Arduino CF\",\n \"cmdmsg\": \"Weekly\",\n \"tempr\": -3\n }\n}","output":"str","x":360,"y":2740,"wires":[["69e91778.e0c6e8","9d8d7da2.2a7da"]]},{"id":"d066800f.60a9b","type":"change","z":"58c8eb7a.5496c4","name":"extract tempr","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.d.tempr","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":2860,"wires":[["af730d72.2995a"]]},{"id":"af730d72.2995a","type":"debug","z":"58c8eb7a.5496c4","name":"tempr","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":630,"y":2860,"wires":[]},{"id":"cf991eb1.f2a1a","type":"debug","z":"58c8eb7a.5496c4","name":"msg object","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":2800,"wires":[]}]
如前所述,您的原始 JSON 字符串无效,因为尾随逗号 -- 但 可以 double-quotes 并且换行,只要它们用“\”转义。解析字符串时,无论如何都会将它们剥离。