赞
踩
已知json对象:{"a":{"b":{"c":{"d":"1111"}}}}
节点路径:a.b.c.d
- function getValByPath(index, keys, obj) {
- if (index == keys.length - 1) {
- return obj[keys[index]];
- }
- return getValByPath(index+1, keys, obj[keys[index]]);
- }
- //调用
- var jsonPath = 'a.b.c.d';
- var arr = keys.split('.');
- var val = getValByPath(0, arr, json);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。