Node Red 에서 장치의 json, xml 형식의 데이터를 처리하는 방법

freeFree Technical Resource

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

Node Red 에서 장치의 json, xml 형식의 데이터를 처리하는 방법

前言

在使用node-red処理各种业务场景时,我们时常会遇到各种各样的データ格式,最も一般的な的是jsondata format,其次还有xml,yaml,以及csv格式的データ格式。这些都是需要我们对其データ格式有一点解析和转化的能力,今天这件の記事我就来教大家如何処理json 和 xmldata format。

node-red中json格式的转换

在node-red処理中ですjsonデータ格式是最简单的。最主要的原因是node-red本身就是使用javascript语言来编写的。而javascript中最もよく使われる就是jsondata format。声明一个jsonデータ格式很简单。

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

json データ格式的操作就是这样的简单。

下面是一个json转换的流,大家可以从这个流中体验的对jsonデータ的操作。

[{"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":[]}]

效果图如下:
Node Red 에서 장치의 json, xml 형식의 데이터를 처리하는 방법Figure
json データ的処理主要是借助来json核心节点,它可以文字列を作る与jsonデータ互转。 非常方便。もし转换失敗就会报错。
json 节点的详细設定如下:

入力参数:
payload:object | ストリング·ストリング
JavaScript对象或JSONストリング·ストリング。
schema:object
可选的JSON Schema对象用于验证有效負荷。在将msg送信到下一个节点之前,将削除该属性。

出力参数:
payload:object | ストリング·ストリング
もし入力是JSONストリング·ストリング,它将尝试将其解析为JavaScript对象。
もし入力是JavaScript对象,它将作成一个JSONストリング·ストリング。并可以選択对此JSONストリング·ストリング进行整形。
schema:Error配列
もしJSONモード验证失敗,则catch节点将具有包含エラー配列的schemaError属性。

node-red処理中です xml格式的データ

在ndoe-red処理中ですxml的データ,主要场景是 jsonデータ変換为xmldata,或者xml格式的データ変換为jsondata format。其中主要借助的是xml核心节点,其底层是使用了xml2json这个库。

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

payload : object | ストリング·ストリング
設定为JavaScript对象或XMLストリング·ストリング。

options:object
可以将选项传递给内部使用的XML转换库。请参见 xml2jsDocument 来取得もっと見る信息。

出力結果是
payload : object | ストリング·ストリング
もし入力是ストリング·ストリング,它将尝试将其解析为XML并作成一个JavaScript对象。
もし入力是JavaScript对象,它将尝试构建XMLストリング·ストリング。

另外在XML和对象之间进行转换时,デフォルト情况下XML属性会追加到名为$的属性中。将文本内容追加到名为_的属性中。这些属性名称可以在节点設定中更改。

下面是一个例子,大家可以从这个例子感受到node-red对xmlデータ与jsonデータ格式的转换

[{"id":"1b546d47.9474e3","type":"inject","z":"64133d39.bb0394","name":"XML String","topic":"","payload":"{"a":1}","payloadType":"str","repeat":"","crontab"]]:"once ": false,"once D elay ": 0. 1," x ": 100," y ": 26 0," wir es ":"d 72 b 2 b f d. 77 d 0 68"]]},{"id ":" 1 ad f 40 7 d. 6 c 4 fe "," type ":"de bug "," z ":" 64 13 3 d 39. bb 0 39 4 "," name ":"once ": true," tos id ebar ": true,"con active ": false," t ost atus ": false,"com ple te ": false "," x ": 59 0," y ": 26 0," wir es ":},{" id":" 46 6 38 89 0. 8 ae 75 8 "," type ":" in ject"," z":" 64 13 3 d 39. bb 0 39 4"," name":" Ob ject"," top ic":" Pay load":"{" note":{"$":{" pri or ity":" high "}," to":" N ick ")," fr om":" Da ve"," he ading":" R em inder"," body":" Web site 업데이트]}}," pay load Ty pe":" j son"," re pe at":" cron tab":" once": false," once D elay": 0. 1," x": 90," y": 300," wir es ":" dae 1 d 29 1. de 0 d 2"]]},{" id":" 6 fe f ca 6 7. 36 69 e 4 "," type":" de bug "," z":" 64 13 3 d 39. bb 0 39 4 "," name":"" active ": true," tos id ebar ": true," con sole ": false," t ost atus ": false," com ple te ": false "," x": 430," y": 300," wir es ":]},{" id":" d 72 b 2 b f d. 77 d 0 68 "," type":" template pay z":" 64 13 3 d 39. bb 0 39 4 "," type" name":" load field "," type":" ms g "," syn tax: " text: " format"" pla in "," temp late":" note priority =" high" n to Nick/to n from Dave/from n heading Rem inder/heading n body 웹 사이트/body n/note 업데이트 "," out put":" str"," x": 28 0," y": 26 0," wir es":" 17 46[[ 464 a. 87 aa 4 a "]]}, {" id ":" 17464 a. 87 aa 4 a "," type ":" xml "," z ":" 64133 d 39. bb 0394 "," name ":" ", "property ":" payload "," attr ":" ", "chr ":" ", "x ":" 430, "y ":" 260, "wires ":" 1 adf 407 d. 6 c 4 fe "]]}, {" id ":"dae 1 d 291. de 0 d 2 "," type ":" xml "," z ":" 64133 d 39. "", " 0394 ", "name ":":":":" payload ", "attr ":" ", "chr ":" x "0, "y ":" 300", wires " 6 fca 67. 36 69 e "]]}][[[[[[[[<><><><><><><><><><>[[[[[[

흐름 도식도
Node Red 에서 장치의 json, xml 형식의 데이터를 처리하는 방법Figure1

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 *.