我有一个JSON字符串:
var data = {"categories":
[
{"id":1,"parent":0,"name":"Category A","description":"Category description","products":"11","subcategories":[]},
{"id":2,"parent":0,"name":"Category B","description":"Category description","products":"11","subcategories":
[
{"id":6,"parent":2,"name":"Subcategory F","description":"Category description", "products":"2","subcategories":[]},
{"id":7,"parent":2,"name":"Subcategory G","description":"Category description","products":"7","subcategories":[]}
]
},
{"id":3,"parent":0,"name":"Category C","description":"Category description","products":"4","subcategories":
[
{"id":8,"parent":3,"name":"Subcategory H","description":"Category description","products":"8","subcategories":[]}
]
},
{"id":4,"parent":0,"name":"Category D","description":"Category description","products":"45","subcategories":
[
{"id":9,"parent":4,"name":"Subcategory I","description":"Category description","products":"2","subcategories":
[
{"id":10,"parent":9,"name":"Subcategory J","description":"Category description","products":"54","subcategories":[]}
]
}
]
},{"id":5,"parent":0,"name":"Category E","description":"Category description","products":"89","subcategories":[]}
]
};
如何通过id访问数据? 例如,我需要获得id = 10的这个sub_object:
var requested = request(data, 10);
function request (data, id) {
var output = {};
...code
output = {"id":10,"parent":9,"name":"Subcategory J","description":"Category description","products":"54","subcategories":[]}
return output;
}
答案 0 :(得分:0)
基本上你可以通过简单的递归来完成:http://jsfiddle.net/99UYU/
var data = {"categories":
[
{"id":1,"parent":0,"name":"Category A","description":"Category description","products":"11","subcategories":[]},
{"id":2,"parent":0,"name":"Category B","description":"Category description","products":"11","subcategories":
[
{"id":6,"parent":2,"name":"Subcategory F","description":"Category description", "products":"2","subcategories":[]},
{"id":7,"parent":2,"name":"Subcategory G","description":"Category description","products":"7","subcategories":[]}
]
},
{"id":3,"parent":0,"name":"Category C","description":"Category description","products":"4","subcategories":
[
{"id":8,"parent":3,"name":"Subcategory H","description":"Category description","products":"8","subcategories":[]}
]
},
{"id":4,"parent":0,"name":"Category D","description":"Category description","products":"45","subcategories":
[
{"id":9,"parent":4,"name":"Subcategory I","description":"Category description","products":"2","subcategories":
[
{"id":10,"parent":9,"name":"Subcategory J","description":"Category description","products":"54","subcategories":[]}
]
}
]
},{"id":5,"parent":0,"name":"Category E","description":"Category description","products":"89","subcategories":[]}
]
};
function recurseArr(arr,id){
for(var i=0;i<arr.length;i++){
var item = arr[i];
if(item.id == id){
return item;
}
var subcategories = item.subcategories;
var ret_val = recurseArr(subcategories,id);
if(ret_val){
return ret_val;
}
}
return null;
}
console.log(recurseArr(data.categories,10));
但是你可以更有用地保存数据,因为你无论如何都在使用id - 所以数组使用对象(map):
var data = {"categories":
{
"1":{"parent":0,"name":"Category A","description":"Category description","products":"11","subcategories":{}},
"2":{"parent":0,"name":"Category B","description":"Category description","products":"11","subcategories":{
"6":{"parent":2,"name":"Subcategory F","description":"Category description", "products":"2","subcategories":{}},
"7":{"parent":2,"name":"Subcategory G","description":"Category description","products":"7","subcategories":{}}
}
}
}
};
然后您可以更轻松地访问您的数据。