一段json可能有很多的子节点,需要查询到某一个节点

用到的js是

find-in-json.js 地址是:https://gist.github.com/iwek/3924925

貌似翻|||墙才能看得到 我还是粘贴出来吧 find-in-json.js完整代码如下:

//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
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);
}
}
}
return objects;
} //return an array of values that match on a certain key
function getValues(obj, key) {
var objects = [];
for (var i in obj) {
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]);
}
}
return objects;
} //return an array of keys that match on a certain value
function getKeys(obj, val) {
var objects = [];
for (var i in obj) {
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);
}
}
return objects;
} 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"]

本人遇到的json各种子节点 最多可能嵌套3、4层 如下:

var findJson = getObjects(jsonObj, 'C0004_Code', '0111');

findJson[0]就是我要找到的对象

json 递归查找某个节点的更多相关文章

  1. Mysql 实现 向上递归查找父节点并返回树结构

    需求:通过mysql 8.0以下版本实现,一个人多角色id,一个角色对应某个节点menu_id,根节点的父节点存储为NULL, 向上递归查找父节点并返回树结构. 如果只有叶子,剔除掉; 如果只有根,只 ...

  2. mysql 递归查找菜单节点的所有子节点

    背景                                                                                                   ...

  3. SQL Server使用WITH AS递归查找

    很多时候我们会在数据库表中存储树结构的数据,如菜单:一级菜单.二级菜单.三级菜单... 如果树结构层次比较多,如何能够在只知道某节点的情况下,找到此节点下的所有子级数据呢? 在.NET后台可以定义一个 ...

  4. ASP.NET递归添加树节点

    表设计如图: id        title         parentid 1         asp.net   0 2         c#           0 3         c#_ ...

  5. 用TinyXml做XML解析示例 TinyXml查找唯一节点及修改节点操作

    // 读者对象:对TinyXml有一定了解的人.本文是对TinyXml工具的一些知识点的理解. // 1 TinyXml中对TiXmlNode进行了分类,是用一个枚举进行描述的. // enum No ...

  6. js 递归获取子节点所有父节点,深度遍历获取第一个子树

    前端需求. 递归 深度优先遍历算法 // 查找一个节点的所有父节点 familyTree (arr1, id) { var temp = [] var forFn = function (arr, i ...

  7. js 查找树节点 数组去重

    //查找树节点function findData(curOrg, id) { var array = []; if ((typeof curOrg == 'object') && (c ...

  8. 查找html节点的方法

    document.firstChild document.documentElement(兼容性较好) 查找body节点的方法 document.firstChild.lastChild docume ...

  9. MySql 获取当前节点及递归所有上级节点

    -- MySql 获取当前节点及递归所有上级节点 -- 参数说明:resultField:查询返回字段,idd 要查询的资源ID值,idFieldName ID字段名,parentIdFieldNam ...

随机推荐

  1. textarea与XSS攻击

    textarea用法 一般是用来接收用户输入,用于提交到服务器端,例如 网站的评论框. 如果此框也用于显示服务器端回传的内容,则有如下两种用法 法1 后台直接插入 <textarea>&l ...

  2. struts action和jsp之间的传值

    一.jsp对Action传值 提交表单即可,Action的属性必须和表单的标签名字相同 二.Action对jsp传值: (1)利用session,(个人推荐) action中ActionContext ...

  3. javascript设计模式简介

  4. 利用jquery获取html中被选中的input的值

    单个按钮 <div id="wrap"> <input type="radio" name="payMethod" val ...

  5. SharpZipLib 文件/文件夹压缩

    一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...

  6. CRM 2011 Install Errors - Tips and Tricks continued(转)

    The more I get to install/upgrade to CRM 2011 in different environment the more I come across differ ...

  7. TextBox

    一.聚焦: private void FrmOnlineChargeMoney_Paint(object sender, PaintEventArgs e) { edtAuthCode.SelectA ...

  8. WPF:常见问题

    1.自定义Main函数 背景: wpf 默认的Main函数在 App.g.cs文件中,在App.xmal.cs内自定义Main函数后冲突. 解决方法: 法一: 1)新建class1.cs类,在其中设置 ...

  9. ASP。net treeview xml

    this.TreeView2.ShowLines = false; //显示连接子节点与父节点之间的线条 TreeNodeBinding area = new TreeNodeBinding(); a ...

  10. Spring中的事物管理,基于spring的bean的配置

    很多东西与上边的相同,这儿只简介: 导包... 数据库中建立三个表... 建立存放连接数据库的file文件:jdbc.properties: ----------------------------- ...