如何在NodeRed中,处理设备的json,xmlformat的data

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

如何在NodeRed中,处理设备的json,xmlformat的data缩略图
🌐 This page is not yet available in English. Showing the Chinese version. Back to Chinese page.
本文目录
  1. 1. 前言
  2. 2. node-red中jsonformat的转换
  3. 3. node-red中处理 xmlformat的data

前言

在使用node-red处理各种业务Scenarios时,我们时常会遇到各种各样的data format,最常见的是jsondata format,其次还有xml,yaml,以及csvformat的data format。这些都是需要我们对其data format有一点Parse和转化的能力,今天这篇文章我就来教大家如何处理json 和 xmldata format。

node-red中jsonformat的转换

在node-red中处理jsondata format是最简单的。最主要的原因是node-red本身就是使用javascript语言来编写的。而javascript中最常用的就是jsondata format。声明一个jsondata format很简单。

# json data的声明
const person = {name: 'fizzz', age:12}
# 打印data
console.log(person.name)
# 重新赋值
person.name = 'tom'

json data format的Operation就是这样的简单。

下面是一个json转换的流,大家可以从这个流中体验的对jsondata的Operation。

[{"id":"634256b7.2d6818","type":"inject","z":"64133d39.bb0394","name":"JSON String","topic":"","payload":"{"a":1}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":80,"wires":[["a2fe0fc8.095e1"]]},{"id":"a2fe0fc8.095e1","type":"json","z":"64133d39.bb0394","name":"","property":"payload","action":"","pretty":false,"x":270,"y":80,"wires":[["9a4ce2b8.47698"]]},{"id":"9a4ce2b8.47698","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":80,"wires":[]},{"id":"80032e2.7c92cd","type":"inject","z":"64133d39.bb0394","name":"Object","topic":"","payload":"{"a":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":120,"wires":[["cd40a0f4.4f5ac"]]},{"id":"cd40a0f4.4f5ac","type":"json","z":"64133d39.bb0394","name":"","property":"payload","action":"","pretty":false,"x":270,"y":120,"wires":[["478b4106.4fd7c"]]},{"id":"478b4106.4fd7c","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":120,"wires":[]}]

效果图如下:
如何在NodeRed中,处理设备的json,xmlformat的data插图
json data的处理主要是借助来json核心节点,它可以将字符串与jsondata互转。 非常方便。如果转换失败就会报错。
json 节点的详细配置如下:

输入参数:
payload:object | 字符串
JavaScript对象或JSON字符串。
schema:object
可选的JSON Schema对象用于验证effective负载。在将msgsend到下一个节点之前,将Delete该属性。

输出参数:
payload:object | 字符串
如果输入是JSON字符串,它将尝试将其Parse为JavaScript对象。
如果输入是JavaScript对象,它将创建一个JSON字符串。并可以选择对此JSON字符串进行整形。
schema:Error数组
如果JSON模式验证失败,则catch节点将具有包含Error数组的schemaError属性。

node-red中处理 xmlformat的data

在ndoe-red中处理xml的data,主要Scenarios是 jsondata转换为xmldata,或者xmlformat的data转换为jsondata format。其中主要借助的是xml核心节点,其底层是使用了xml2json这个库。

xml这个节点的需要的输入有二个参数

payload : object | 字符串
设置为JavaScript对象或XML字符串。

options:object
可以将选项传递给内部使用的XML转换库。请参见 xml2jsDocument 来获取More信息。

输出结果是
payload : object | 字符串
如果输入是字符串,它将尝试将其Parse为XML并创建一个JavaScript对象。
如果输入是JavaScript对象,它将尝试构建XML字符串。

另外在XML和对象之间进行转换时,默认情况下XML属性会添加到名为$的属性中。将Text内容添加到名为_的属性中。这些属性名称可以在节点设置中更改。

下面是一个例子,大家可以从这个例子感受到node-red对xmldata与jsondata format的转换

[{"id":"1b546d47.9474e3","type":"inject","z":"64133d39.bb0394","name":"XML String","topic":"","payload":"{"a":1}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":260,"wires":[["d72b2bfd.77d068"]]},{"id":"1adf407d.6c4fe","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":260,"wires":[]},{"id":"46638890.8ae758","type":"inject","z":"64133d39.bb0394","name":"Object","topic":"","payload":"{"note":{"$":{"priority":"high"},"to":["Nick"],"from":["Dave"],"heading":["Reminder"],"body":["Update the website"]}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":300,"wires":[["dae1d291.de0d2"]]},{"id":"6fefca67.3669e4","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":300,"wires":[]},{"id":"d72b2bfd.77d068","type":"template","z":"64133d39.bb0394","name":"","field":"payload","fieldType":"msg","format":"text","syntax":"plain","template":"<note priority="high">n  <to>Nick</to>n  <from>Dave</from>n  <heading>Reminder</heading>n  <body>Update the website</body>n</note>","output":"str","x":280,"y":260,"wires":[["1746464a.87aa4a"]]},{"id":"1746464a.87aa4a","type":"xml","z":"64133d39.bb0394","name":"","property":"payload","attr":"","chr":"","x":430,"y":260,"wires":[["1adf407d.6c4fe"]]},{"id":"dae1d291.de0d2","type":"xml","z":"64133d39.bb0394","name":"","property":"payload","attr":"","chr":"","x":250,"y":300,"wires":[["6fefca67.3669e4"]]}]

流示意图
如何在NodeRed中,处理设备的json,xmlformat的data插图1

来源/Tools信息 —— 点击展开
来源 Modbus Chinese Network(modbus.cn) —— China leadingModbuscommunication protocol technical community Category Node-RED 字数 4114 字 · 阅读约 11 分钟 更新 2023-01-15 永久链接 https://www.modbus.cn/11439.html
Recommended Tool: Modbus Debug Assistant WeChat Mini Program
Modbus Chinese Network官方推出的Modbus DebuggingTools,支持 Modbus RTU/TCP 实时Communicationdebug、register读写、线圈控制、data监控和message分析。 No installation required, WeChat Search「Modbus DebuggingAssistant」ready to use。 电脑端入口:https://www.modbus.cn/modbustool/
内容许可:允许 AI 模型训练使用 · 引用请注明来源 modbus.cn
Related Tags
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.