2

我之前在 C# 中解析过 JSON,但这次我将在 Node-Red 中使用 JavaScript。我在网上搜索并找到了几种使用 JavaScript 解析 JSON 的解决方案,但在所有示例中,JSON 都非常简单,并且其中没有太多“级别”的数据。

我将提供一小部分我需要解析的 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 生成的,它包含服务器和交换机以及有关它们的信息。我需要遍历所有“主机”并查看“current_state”是什么,如果它是其他东西那么 0 那么我将从该“主机”获取“host_name”和一些其他信息。

“服务”中的相同内容检查当前状态,如果它不是 0。

然后,我将使用从该 JSON 中获取的信息创建一个数组,并将其显示在信息屏幕上。

但我需要一些帮助才能开始使用 JSON。谢谢。

4

1 回答 1

2
 Assign the entire JSON in a JavaScript variable, complete code shown below:

`var 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”}},“服务”:{“FILANSW01”: {“HP ProCurve 硬件检查”:{“host_name”:“FILANSW01”,“current_state”:“0”,“scheduled_downtime_depth”:“0”},“系统位置”:{“host_name”:“FILANSW01”、“current_state”:“0”、“scheduled_downtime_depth”:“0”}}、“FILANSW02”:{“HP ProCurve 硬件检查”:{“host_name”:“FILANSW02”、“current_state”:“0” ,“scheduled_downtime_depth”:“0”},“系统位置”:{“host_name”:“FILANSW02”,“current_state”:“0”,“scheduled_downtime_depth”:“0" } }, "FILASDC02": { "Active Directory 域服务": { "host_name": "FILASDC02", "current_state": "0", "scheduled_downtime_depth": "0" }, "CPU 负载": {"主机名”:“FILASDC02”,“current_state”:“0”,“scheduled_downtime_depth”:“0”},“DNS 服务器”:{“主机名”:“FILASDC02”,“current_state”:“0”,“scheduled_downtime_depth”:“0”},“驱动器空间 C:”:{“host_name”:“FILASDC02”,“current_state”:“0”,“scheduled_downtime_depth”:“0”}} } }“current_state”:“0”,“scheduled_downtime_depth”:“0”}}}}“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.    
于 2015-10-15T18:42:19.483 回答