html 查找json 节点,json 递归查找某个节点

//return an array of objects according to key, value, or key and value matching

functiongetObjects(obj, key, val) {var objects =[];for (var i inobj) {if (!obj.hasOwnProperty(i)) continue;if (typeof obj[i] == 'object') {

objects=objects.concat(getObjects(obj[i], key, val));

}else

//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)

if (i == key && obj[i] == val || i == key && val == '') { //objects.push(obj);

}else if (obj[i] == val && key == ''){//only add if the object is not already in the array

if (objects.lastIndexOf(obj) == -1){

objects.push(obj);

}

}

}returnobjects;

}//return an array of values that match on a certain key

functiongetValues(obj, key) {var objects =[];for (var i inobj) {if (!obj.hasOwnProperty(i)) continue;if (typeof obj[i] == 'object') {

objects=objects.concat(getValues(obj[i], key));

}else if (i ==key) {

objects.push(obj[i]);

}

}returnobjects;

}//return an array of keys that match on a certain value

functiongetKeys(obj, val) {var objects =[];for (var i inobj) {if (!obj.hasOwnProperty(i)) continue;if (typeof obj[i] == 'object') {

objects=objects.concat(getKeys(obj[i], val));

}else if (obj[i] ==val) {

objects.push(i);

}

}returnobjects;

}var json = '{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","ID":"44","str":"SGML","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}';var js =JSON.parse(json);//example of grabbing objects that match some key and value in JSON

console.log(getObjects(js,'ID','SGML'));//returns 1 object where a key names ID has the value SGML

//example of grabbing objects that match some key in JSON

console.log(getObjects(js,'ID',''));//returns 2 objects since keys with name ID are found in 2 objects

//example of grabbing obejcts that match some value in JSON

console.log(getObjects(js,'','SGML'));//returns 2 object since 2 obects have keys with the value SGML

//example of grabbing objects that match some key in JSON

console.log(getObjects(js,'ID',''));//returns 2 objects since keys with name ID are found in 2 objects

//example of grabbing values from any key passed in JSON

console.log(getValues(js,'ID'));//returns array ["SGML", "44"]

//example of grabbing keys by searching via values in JSON

console.log(getKeys(js,'SGML'));//returns array ["ID", "SortAs", "Acronym", "str"]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值