node-RED json 对象 - 如何提取 json 值 [英] node-RED json object - how to extract json values
问题描述
在 node-RED 中,我使用batcher"节点将 2 个湿度读数汇总到一个 msg 中,我希望以 json 格式将其用于下游,一个用于内部,另一个用于四边形.
In node-RED, I used 'batcher' node to bring 2 Humidity readings together in a msg, which I want in a json format, to use it downstream, one for inside, there other in a quad.
构造的 msg 对象现在看起来像这样:
The constructed msg object now looks like this:
{ "topic": "Hum", "payload": [ { "inside": 29 }, { "quad": 54 } ] }
我正在尝试访问此对象中的元素:
I am trying to get to elements in this object, with:
msg.payload.inside
但它不起作用,我怎样才能获得例如内部湿度?
But it is not working, how can I get to for instance the inside humidity ?
推荐答案
该对象的 payload
属性是一个包含两个元素的数组.
The payload
property of that object is an Array that contains two elements.
如果要访问该数组中对象的值,则需要对其进行索引.
If you want to access the value of an object inside that array, you need to index into it.
数组从 0
开始索引 - 所以你想使用 msg.payload[0].inside
Arrays are indexed from 0
- so you want to use msg.payload[0].inside
请注意,这是标准 JavaScript,并非特定于 Node-RED.
Note that this is standard JavaScript and not specific to Node-RED.
这篇关于node-RED json 对象 - 如何提取 json 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!