我之前在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。 感谢。
答案 0 :(得分: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" }, "主持人":{ " 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负载":{ " host_name":" FILASDC02", " current_state":" 0", " scheduled_downtime_depth":" 0" }, " DNS服务器":{ " host_name":" FILASDC02", " current_state":" 0", " scheduled_downtime_depth":" 0" }, "驱动器空间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.