我正在尝试从json文件中解析和读取值,并将其转换为HTML。在下面的问题中,我希望我的div重复出现“ No of times”警报属性。例如,我想知道当前json对象有多少次“ 警报类别:优先级11 ”。
我已经使用jquery getjson来解析文件。
Javascript代码
$(document).ready(function() {
$.getJSON("XL2.json", function(data) {
var arritems = [];
var getV = 0;
var getAlarmClass = 0;
$.each(data, function(key, val) {
if (val["Alarm Class"] == "Priority 1") {
arritems.push(val);
return (getAlarmClass = arritems.length);
}
});
$(
"<p> Total Alarm for Priority 1 is : " + getAlarmClass + "</p>"
).appendTo(".getCount");
});
});
Json文本示例
[
{
"": "",
" ": "",
"Source State": "Normal",
"Message": "FOOD FREEZER - NORMAL",
"Priority": "255",
"Ack State": "0 Acked / 1 Unacked",
"Timestamp": "16-May-19 5:24:28 AM PDT",
"Source": "C_PETCOxL_1114xaSA_RILCxTS_C7xaP_TMP",
"Alarm Class": "Priority 15"
},
{
"": "",
" ": "",
"Source State": "Offnormal",
"Message": "SECALM - ALARM",
"Priority": "255",
"Ack State": "0 Acked / 1 Unacked",
"Timestamp": "16-May-19 5:22:58 AM PDT",
"Source": "C_PETCOxL_2145xaP_SECALM",
"Alarm Class": "Priority 11"
},
{
"": "",
" ": "",
"Source State": "Normal",
"Message": "ZONE TEMP - NORMAL",
"Priority": "255",
"Ack State": "0 Acked / 1 Unacked",
"Timestamp": "16-May-19 5:21:28 AM PDT",
"Source": "L_1904xRTUS_02xaRTU_05xZNTMPALM01",
"Alarm Class": "Priority 2"
},
{
"": "",
" ": "",
"Source State": "Normal",
"Message": "ZONE TEMP - NORMAL",
"Priority": "255",
"Ack State": "0 Acked / 1 Unacked",
"Timestamp": "16-May-19 5:21:12 AM PDT",
"Source": "C_PETCOxL_1278xaSA_GRMGxTS_05xaP_TMPALM",
"Alarm Class": "Priority 2"
},
{
"": "",
" ": "",
"Source State": "Offnormal",
"Message": "SECALM - ALARM",
"Priority": "255",
"Ack State": "0 Acked / 2 Unacked",
"Timestamp": "16-May-19 5:15:38 AM PDT",
"Source": "C_PETCOxL_3821xaP_SECALM",
"Alarm Class": "Priority 11"
},
{
"": "",
" ": "",
"Source State": "Fault",
"Message": "RTU - FAULT",
"Priority": "255",
"Ack State": "0 Acked / 4 Unacked",
"Timestamp": "16-May-19 5:08:51 AM PDT",
"Source": "C_PETCOxL_2120xRTUS_01xRTU_03xaRTU$20MODE",
"Alarm Class": "Priority 6"
}
]
输出
我的HTML输出应显示
优先级11:3(3是XL2.json文件中优先级11出现的次数)