在 Node-Red 中解析 JSON javascript
parse JSON javascript in Node-Red
我之前在 C# 中解析过 JSON 但这次我打算在 Node-Red 中使用 JavaScript。
我在网上搜索并找到了几个用 JavaScript 解析 JSON 的解决方案,但在所有示例中,JSON 非常简单,其中的数据不多 "level"。
我将提供我需要解析的 JSON 文件的一小部分
{
"programStatus": {
"modified_host_attributes": "1",
"modified_service_attributes": "1",
"serial_host_check_stats": "0,0,0"
},
"hosts": {
"FILANAS01": {
"host_name": "FILANAS01",
"modified_attributes": "0",
"check_command": "check-host-alive",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"FILANAS02": {
"host_name": "FILANAS02",
"modified_attributes": "0",
"check_command": "check-host-alive",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"FILANSW01": {
"host_name": "FILANSW01",
"modified_attributes": "0",
"check_command": "check-host-alive",
"scheduled_downtime_depth": "0"
},
"FILANSW02": {
"host_name": "FILANSW02",
"modified_attributes": "0",
"check_command": "check-host-alive",
"current_state": "0",
"scheduled_downtime_depth": "0"
}
},
"services": {
"FILANSW01": {
"HP ProCurve Hardware Check": {
"host_name": "FILANSW01",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"System Location": {
"host_name": "FILANSW01",
"current_state": "0",
"scheduled_downtime_depth": "0"
}
},
"FILANSW02": {
"HP ProCurve Hardware Check": {
"host_name": "FILANSW02",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"System Location": {
"host_name": "FILANSW02",
"current_state": "0",
"scheduled_downtime_depth": "0"
}
},
"FILASDC02": {
"Active Directory Domain Services": {
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"CPU Load": {
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"DNS Server": {
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"Drive Space C:": {
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth": "0"
}
}
}
}
这是从 Nagios 生成的,它包含服务器和交换机以及有关它们的信息。
我需要遍历所有 "hosts" 并查看 "current_state" 是什么,如果它是其他东西然后 0 那么我将从 "host" 中获取 "host_name" 和一些其他信息。
和 "services" 中的相同内容检查当前状态,如果它不是 0。
然后我将使用我从中获取的信息创建一个数组 JSON 并将其显示在信息屏幕上。
但我需要一些帮助才能开始使用 JSON。
谢谢
Assign the entire JSON in a JavaScript variable, complete code shown below:
`
变量 myJSON = {
"programStatus":{
"modified_host_attributes": "1",
"modified_service_attributes": "1",
"serial_host_check_stats": "0,0,0"
},
"hosts":{
"FILANAS01":{
"host_name": "FILANAS01",
"modified_attributes": "0",
"check_command": "check-host-alive",
"current_state": "0",
"scheduled_downtime_depth":“0”
},
"FILANAS02":{
"host_name": "FILANAS02",
"modified_attributes": "0",
"check_command": "check-host-alive",
"current_state": "0",
"scheduled_downtime_depth":“0”
},
"FILANSW01":{
"host_name": "FILANSW01",
"modified_attributes": "0",
"check_command": "check-host-alive",
"scheduled_downtime_depth":“0”
},
"FILANSW02":{
"host_name": "FILANSW02",
"modified_attributes": "0",
"check_command": "check-host-alive",
"current_state": "0",
"scheduled_downtime_depth":“0”
}
},
"services":{
"FILANSW01":{
"HP ProCurve Hardware Check":{
"host_name": "FILANSW01",
"current_state": "0",
"scheduled_downtime_depth":“0”
},
"System Location":{
"host_name": "FILANSW01",
"current_state": "0",
"scheduled_downtime_depth":“0”
}
},
"FILANSW02":{
"HP ProCurve Hardware Check":{
"host_name": "FILANSW02",
"current_state": "0",
"scheduled_downtime_depth":“0”
},
"System Location":{
"host_name": "FILANSW02",
"current_state": "0",
"scheduled_downtime_depth":“0”
}
},
"FILASDC02":{
"Active Directory Domain Services":{
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth":“0”
},
"CPU Load":{
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth":“0”
},
"DNS Server":{
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth":“0”
},
"Drive Space C:":{
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth":“0”
}
}
}
}
Now iterate through:
var host = Object.keys(myJSON["hosts"]);
for (var i = 0; i < host.length; i++) {
var hostData = host[i];
var hostProp = myJSON.hosts[hostData];
if (hostProp.current_state != 0) {
//Do your work.
}
}
I've tested Object.keys() in a few browsers like IE9, IE10, Chrome 46
and FireFox, it works but fails in IE8.
我之前在 C# 中解析过 JSON 但这次我打算在 Node-Red 中使用 JavaScript。 我在网上搜索并找到了几个用 JavaScript 解析 JSON 的解决方案,但在所有示例中,JSON 非常简单,其中的数据不多 "level"。
我将提供我需要解析的 JSON 文件的一小部分
{
"programStatus": {
"modified_host_attributes": "1",
"modified_service_attributes": "1",
"serial_host_check_stats": "0,0,0"
},
"hosts": {
"FILANAS01": {
"host_name": "FILANAS01",
"modified_attributes": "0",
"check_command": "check-host-alive",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"FILANAS02": {
"host_name": "FILANAS02",
"modified_attributes": "0",
"check_command": "check-host-alive",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"FILANSW01": {
"host_name": "FILANSW01",
"modified_attributes": "0",
"check_command": "check-host-alive",
"scheduled_downtime_depth": "0"
},
"FILANSW02": {
"host_name": "FILANSW02",
"modified_attributes": "0",
"check_command": "check-host-alive",
"current_state": "0",
"scheduled_downtime_depth": "0"
}
},
"services": {
"FILANSW01": {
"HP ProCurve Hardware Check": {
"host_name": "FILANSW01",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"System Location": {
"host_name": "FILANSW01",
"current_state": "0",
"scheduled_downtime_depth": "0"
}
},
"FILANSW02": {
"HP ProCurve Hardware Check": {
"host_name": "FILANSW02",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"System Location": {
"host_name": "FILANSW02",
"current_state": "0",
"scheduled_downtime_depth": "0"
}
},
"FILASDC02": {
"Active Directory Domain Services": {
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"CPU Load": {
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"DNS Server": {
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth": "0"
},
"Drive Space C:": {
"host_name": "FILASDC02",
"current_state": "0",
"scheduled_downtime_depth": "0"
}
}
}
}
这是从 Nagios 生成的,它包含服务器和交换机以及有关它们的信息。 我需要遍历所有 "hosts" 并查看 "current_state" 是什么,如果它是其他东西然后 0 那么我将从 "host" 中获取 "host_name" 和一些其他信息。
和 "services" 中的相同内容检查当前状态,如果它不是 0。
然后我将使用我从中获取的信息创建一个数组 JSON 并将其显示在信息屏幕上。
但我需要一些帮助才能开始使用 JSON。 谢谢
Assign the entire JSON in a JavaScript variable, complete code shown below:
` 变量 myJSON = { "programStatus":{ "modified_host_attributes": "1", "modified_service_attributes": "1", "serial_host_check_stats": "0,0,0" }, "hosts":{ "FILANAS01":{ "host_name": "FILANAS01", "modified_attributes": "0", "check_command": "check-host-alive", "current_state": "0", "scheduled_downtime_depth":“0” }, "FILANAS02":{ "host_name": "FILANAS02", "modified_attributes": "0", "check_command": "check-host-alive", "current_state": "0", "scheduled_downtime_depth":“0” }, "FILANSW01":{ "host_name": "FILANSW01", "modified_attributes": "0", "check_command": "check-host-alive", "scheduled_downtime_depth":“0” }, "FILANSW02":{ "host_name": "FILANSW02", "modified_attributes": "0", "check_command": "check-host-alive", "current_state": "0", "scheduled_downtime_depth":“0” } }, "services":{ "FILANSW01":{ "HP ProCurve Hardware Check":{ "host_name": "FILANSW01", "current_state": "0", "scheduled_downtime_depth":“0” }, "System Location":{ "host_name": "FILANSW01", "current_state": "0", "scheduled_downtime_depth":“0” } }, "FILANSW02":{ "HP ProCurve Hardware Check":{ "host_name": "FILANSW02", "current_state": "0", "scheduled_downtime_depth":“0” }, "System Location":{ "host_name": "FILANSW02", "current_state": "0", "scheduled_downtime_depth":“0” } }, "FILASDC02":{ "Active Directory Domain Services":{ "host_name": "FILASDC02", "current_state": "0", "scheduled_downtime_depth":“0” }, "CPU Load":{ "host_name": "FILASDC02", "current_state": "0", "scheduled_downtime_depth":“0” }, "DNS Server":{ "host_name": "FILASDC02", "current_state": "0", "scheduled_downtime_depth":“0” }, "Drive Space C:":{ "host_name": "FILASDC02", "current_state": "0", "scheduled_downtime_depth":“0” } } } }
Now iterate through:
var host = Object.keys(myJSON["hosts"]);
for (var i = 0; i < host.length; i++) {
var hostData = host[i];
var hostProp = myJSON.hosts[hostData];
if (hostProp.current_state != 0) {
//Do your work.
}
}
I've tested Object.keys() in a few browsers like IE9, IE10, Chrome 46
and FireFox, it works but fails in IE8.