如何解析Node-RED函数中的JSON [英] How Can I parse out JSON in a Node-RED Function

查看:1798
本文介绍了如何解析Node-RED函数中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Bluemix for IoT中的Node-RED.

I am working with Node-RED in Bluemix for IoT.

我如何解析功能节点中的各个信息(例如cmdmsg和tempr),以便可以在流中的其他节点中使用它? 尝试时出现错误(见下文)

How can I parse out the individual pieces of information (like the cmdmsg and the tempr) in a function node so I can use it in other nodes in the flow? I'm getting an error when I try (see below)

当我将其设置为查看完整的消息对象时,我从调试"节点中看到了JSON完整的消息对象(来自Node中的IoT).参见下面的对象.

I am receiving the JSON complete message object (from an IoT in Node) that I see in my "debug" node when I set it to look at the complete message object. see the object below.

在我看来JSON的格式正确.

It appears to me that the JSON is formatted correctly.

我尝试将以下内容放入功能节点,但出现错误,提示"TypeError:无法读取未定义的属性'tempr'"

I tried putting the following in the function node, but I'm getting an error that says "TypeError: Cannot read property 'tempr' of undefined"

以下是function参数的含义: 返回{payload:msg.payload.d.tempr};

Here is what the function parameter is: return {payload: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

note: I obfuscated the device ID (mac address) and msgid

关于如何解析数据以及为什么出现错误的任何想法?

Any ideas on how to parse the data out and why I'm getting an error?

推荐答案

对不起,但是您的JSON有效负载已完全搞砸了,它看起来应该像这样:{"d": {"myName": "Arduino CF","cmdmsg": "Weekly","tempr": -3}} 您不应在有效负载中看到任何\\n,它们看起来像是来自客户端的转义字符.我还认为,对于有效的JSON,不应在tempr值之后添加最后一个逗号.

Sorry, but your JSON Payload is completely messed up, it should look like this: {"d": {"myName": "Arduino CF","cmdmsg": "Weekly","tempr": -3}} You shouldn't see any \or \nin the payload, they look like escape characters from the client side. I also believe that the last comma after the tempr value shouldn't be there for valid 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"}}

I am not an Arduino expert but I have experimented with a Raspberry Pi and the Mosquitto client, this is how I can successfully send an event to 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};将起作用.

If the paylod is correct JSON your statement return {payload:msg.payload.d.tempr}; will work.

您是否看到过以下内容: http://www. ibm.com/developerworks/cloud/library/cl-bluemix-arduino-iot2/

Have you seen this: http://www.ibm.com/developerworks/cloud/library/cl-bluemix-arduino-iot2/

这篇关于如何解析Node-RED函数中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆