根据条件获取json(无限级)中指定的对象
JSON:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[
{"id":"1","text":"电脑整机","parentid":"0","children":[
{"id":"2","text":"笔记本","parentid":"1","children":[
{"id":"31","text":"SONY","parentid":"2","children":[]},
{"id":"23","text":"LENOVO","parentid":"2","children":[]},
{"id":"25","text":"IBM","parentid":"2","children":[]},
{"id":"26","text":"宏基","parentid":"2","children":[]},
{"id":"27","text":"联想","parentid":"2","children":[]},
{"id":"28","text":"联想2","parentid":"2","children":[]}
]},
{"id":"3","text":"上网本","parentid":"1","children":[]},
{"id":"4","text":"平板电脑","parentid":"1","children":[]},
{"id":"5","text":"台式机","parentid":"1","children":[]},
{"id":"6","text":"服务器","parentid":"1","children":[]}
]},
{"id":"7","text":"电脑配件","parentid":"0","children":[
{"id":"8","text":"CPU","parentid":"7","children":[]},
{"id":"9","text":"主板","parentid":"7","children":[]},
{"id":"10","text":"显卡","parentid":"7","children":[]},
{"id":"11","text":"硬盘","parentid":"7","children":[]},
{"id":"12","text":"内存","parentid":"7","children":[]},
{"id":"13","text":"机箱","parentid":"7","children":[]},
{"id":"14","text":"电源","parentid":"7","children":[]},
{"id":"15","text":"显示器","parentid":"7","children":[]},
{"id":"16","text":"刻录机/光驱","parentid":"7","children":[]},
{"id":"17","text":"声卡","parentid":"7","children":[]},
{"id":"18","text":"扩展卡","parentid":"7","children":[]}
]}
]
{"id":"1","text":"电脑整机","parentid":"0","children":[
{"id":"2","text":"笔记本","parentid":"1","children":[
{"id":"31","text":"SONY","parentid":"2","children":[]},
{"id":"23","text":"LENOVO","parentid":"2","children":[]},
{"id":"25","text":"IBM","parentid":"2","children":[]},
{"id":"26","text":"宏基","parentid":"2","children":[]},
{"id":"27","text":"联想","parentid":"2","children":[]},
{"id":"28","text":"联想2","parentid":"2","children":[]}
]},
{"id":"3","text":"上网本","parentid":"1","children":[]},
{"id":"4","text":"平板电脑","parentid":"1","children":[]},
{"id":"5","text":"台式机","parentid":"1","children":[]},
{"id":"6","text":"服务器","parentid":"1","children":[]}
]},
{"id":"7","text":"电脑配件","parentid":"0","children":[
{"id":"8","text":"CPU","parentid":"7","children":[]},
{"id":"9","text":"主板","parentid":"7","children":[]},
{"id":"10","text":"显卡","parentid":"7","children":[]},
{"id":"11","text":"硬盘","parentid":"7","children":[]},
{"id":"12","text":"内存","parentid":"7","children":[]},
{"id":"13","text":"机箱","parentid":"7","children":[]},
{"id":"14","text":"电源","parentid":"7","children":[]},
{"id":"15","text":"显示器","parentid":"7","children":[]},
{"id":"16","text":"刻录机/光驱","parentid":"7","children":[]},
{"id":"17","text":"声卡","parentid":"7","children":[]},
{"id":"18","text":"扩展卡","parentid":"7","children":[]}
]}
]
js:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var obj;
function searchtree(nodes,id) {
$.each(nodes, function (i, n) {
if (n.id == id) {
obj = n;
return false;
}
else {
if (n.children.length > 0) {
searchtree(n.children, id);
}
}
});
return obj;
}
$.each(nodes, function (i, n) {
if (n.id == id) {
obj = n;
return false;
}
else {
if (n.children.length > 0) {
searchtree(n.children, id);
}
}
});
return obj;
}