json 递归查找某个节点
一段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 递归查找某个节点的更多相关文章
- Mysql 实现 向上递归查找父节点并返回树结构
需求:通过mysql 8.0以下版本实现,一个人多角色id,一个角色对应某个节点menu_id,根节点的父节点存储为NULL, 向上递归查找父节点并返回树结构. 如果只有叶子,剔除掉; 如果只有根,只 ...
- mysql 递归查找菜单节点的所有子节点
背景 ...
- SQL Server使用WITH AS递归查找
很多时候我们会在数据库表中存储树结构的数据,如菜单:一级菜单.二级菜单.三级菜单... 如果树结构层次比较多,如何能够在只知道某节点的情况下,找到此节点下的所有子级数据呢? 在.NET后台可以定义一个 ...
- ASP.NET递归添加树节点
表设计如图: id title parentid 1 asp.net 0 2 c# 0 3 c#_ ...
- 用TinyXml做XML解析示例 TinyXml查找唯一节点及修改节点操作
// 读者对象:对TinyXml有一定了解的人.本文是对TinyXml工具的一些知识点的理解. // 1 TinyXml中对TiXmlNode进行了分类,是用一个枚举进行描述的. // enum No ...
- js 递归获取子节点所有父节点,深度遍历获取第一个子树
前端需求. 递归 深度优先遍历算法 // 查找一个节点的所有父节点 familyTree (arr1, id) { var temp = [] var forFn = function (arr, i ...
- js 查找树节点 数组去重
//查找树节点function findData(curOrg, id) { var array = []; if ((typeof curOrg == 'object') && (c ...
- 查找html节点的方法
document.firstChild document.documentElement(兼容性较好) 查找body节点的方法 document.firstChild.lastChild docume ...
- MySql 获取当前节点及递归所有上级节点
-- MySql 获取当前节点及递归所有上级节点 -- 参数说明:resultField:查询返回字段,idd 要查询的资源ID值,idFieldName ID字段名,parentIdFieldNam ...
随机推荐
- Ubuntu下编译运行C#——mono tools
编译C#代码用mono-csc,直接编译成二进制可执行文件: mono-csc a.cs b.cs c.cs d.cs 如果一个工程里文件很多,可以使用通配符“*”: mono-csc *.cs
- Scrum项目4.0
4.0----------------------------------------------- 1.准备看板. 形式参考图4. 2.任务认领,并把认领人标注在看板上的任务标签上. 先由个人主动领 ...
- C#学校班级自动升级实现代码
代码逻辑如下: //班级自动升级 //获取该学校还没有毕业的班级 List<ClassInfoes> classinfoeslist = classinfoesbll.GetList(Sc ...
- opencv的学习笔记1
想在周末去游泳,找了些游泳的注意事项什么的,想想还没干正事,就来继续看看opencv的使用吧,晚上看了opencv的一些基本入门的东西,打算下面主要总结CSDN上一个大牛的博文,链接如下:http:/ ...
- 关于ScrollView和listview的冲突关于的滑动和宽度
listview和ScrollView嵌套有两个冲突,关于listview显示不全的问题和listview和scrollview的滑动冲突 自定义listview package com.exmple ...
- 使用cygwin出现syntax error near unexpected token'$'do\r
直接从csdn复制粘贴的.sh代码,放到cygwin下运行sh的时候出错syntax error near unexpected token'$'do\r 解决方法: 1.下载notepad++ 2. ...
- Android布局-TableLayout表格布局
一.表格布局-TableLayout 1.概念 表格布局采用行列的形式来管理UI的控件.表格布局适合于有规则的布局. TableRow,用来管理行,TableRow中的一个空间占据该行的一列.若不用T ...
- 深入理解为什么Java中方法内定义的内部类可以访问方法中的局部变量
好文转载:http://blog.csdn.net/zhangjg_blog/article/details/19996629 开篇 在我的上一篇博客 深入理解Java中为什么内部类可以访问外部类的成 ...
- 如何阅读《ECMAScript 2015 Language Specification》
你不需要把<ECMAScript 2015 Language Specification>通读一遍,因为没那个必要. 阮一峰建议: 对于一般用户来说,除了第4章,其他章节都涉及某一方面 ...
- WSDL2ObjC Unsupported Media Type
调用WCF服务时,出这样的异常“415 Unsupported Media Type”, Because the WCF soap is v1.1, the http header should be ...