JS获取Json值以及通过值获取索引
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
我的JSON格式是这样(大概截取一段展示下):
"result":{
"total":179,
"list":[
{
"id":"b8:27:eb:8d:dd:c8",
"hostname":"2l3f-wt-board",
"osversion":"3.5.1",
"osbuild":"14009",
"resolution":"1360x768",
"ip":"10.99.105.156",
"monitor":"SHARP_HDMI",
"lastupdate":1533607224293,
"append":{
"lostheartbeat":false,
"cputemperature":61.2
}
},
{
"id":"b8:27:eb:c4:d7:6c",
"hostname":"D-Office",
"osversion":"3.5.1",
"osbuild":"14009",
"resolution":"1360x768",
"ip":"10.99.67.141",
"monitor":"5000_Series",
"lastupdate":1533607215300,
"append":{
"lostheartbeat":false,
"cputemperature":52.6
}
},
{
"id":"b8:27:eb:97:a9:5a",
"hostname":"SCC-Andon",
"osversion":"3.5.1",
"osbuild":"14009",
"resolution":"1920x1080",
"ip":"169.254.88.215",
"monitor":"Mi_TV",
"lastupdate":1533607214146,
"append":{
"lostheartbeat":false,
"cputemperature":41.9
}
},
]
}
首先我们来获取对应的值,比如获取ip:
//将JSON转化下
data = $.parseJSON(TextBox1.value);
//遍历json
for (i in data) {
var list = data[i].list
for (a in list) {
list[a].ip
}
}
反之如果我知道ip多少,如何查找该ip对应的其他key的值呢?
思路:
1,我们可以将ip先一次性全部从json中拿出来且转换成数组:
//将JSON转化下
data = $.parseJSON(TextBox1.value);
//遍历json
for (i in data) {
var list = data[i].list
var AllIp;
for (a in list) {
AllIp += list[a].ip + ",";
}
var IPArry = AllIp.split(",");
}
2,如上代码IPArry 即获取到所有ip,现在就好办了,知道ip,就很容易获取器索引了:
var result = IPArry.indexOf(‘iP’);
3,获取到索引,那就很容易获取到该ip对应的其他key的值了
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
3 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)