简体   繁体   English

Node-RED 解析 json

[英]Node-RED parse json

I am trying to pull out the value "533"我正在尝试提取值“533”

{
  "d": {
    "ItemValue_1": 533
   },
   "ts": "2021-01-20T10:59:41.958591"
}

This does not work这不起作用

var ItemValue_1 = msg.payload.ItemValue_1;
msg.payload = ItemValue_1;
return msg;

My result is unsuccessful我的结果不成功

I was able to solve on my own, it works.我能够自己解决,它有效。

sensorMeasurement=JSON.parse(msg.payload);
msg.payload=sensorMeasurement.d;
var msgout = {payload : msg.payload.ItemValue_1}
return msgout;

The better way to do this is as follows:更好的方法如下:

Add a JSON node before the function node, this will turn a string payload in to a JSON object (assuming the string actually represents a JSON object). Add a JSON node before the function node, this will turn a string payload in to a JSON object (assuming the string actually represents a JSON object).

Then if you are using a function node the following:然后,如果您使用的是 function 节点,请执行以下操作:

msg.payload = msg.payload.d.ItemValue_1;
return msg;

It is bad practice to create a new object as you did in your answer as it throws away any meta data that may be required later that is attached to the original object.像您在答案中所做的那样创建新的 object 是一种不好的做法,因为它会丢弃以后可能需要的附加到原始 object 的任何元数据。

Rather than use a function node it would also be cleaner to use a change node and the Move mode to shift the msg.payload.d.ItemValue_1 to msg.payload与其使用 function 节点,还不如使用更改节点和移动模式将msg.payload.d.ItemValue_1转移到msg.payload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM