使用jQuery搜索JSON对象
我需要查询一个类似于JSON数组的javascript对象。我想知道是否有一种方法可以使用jQuery来查找节点值,而无需迭代整个对象并顺序测试每个节点是否匹配。例如,以下是对象:使用jQuery搜索JSON对象,jquery,Jquery,我需要查询一个类似于JSON数组的javascript对象。我想知道是否有一种方法可以使用jQuery来查找节点值,而无需迭代整个对象并顺序测试每个节点是否匹配。例如,以下是对象: Test = {}; Test.Species = { "Category": [{ "Product": [{ "id": "a01", "name": "Pine", "description": "Short de
Test = {};
Test.Species = {
"Category": [{
"Product": [{
"id": "a01",
"name": "Pine",
"description": "Short description of pine."
},
{
"id": "a02",
"name": "Birch",
"description": "Short description of birch."
},
{
"id": "a03",
"name": "Poplar",
"description": "Short description of poplar."
}],
"id": "A",
"title": "Cheap",
"description": "Short description of category A."
},
{
"Product": [{
"id": "b01",
"name": "Maple",
"description": "Short description of maple."
},
{
"id": "b02",
"name": "Oak",
"description": "Short description of oak."
},
{
"id": "b03",
"name": "Bamboo",
"description": "Short description of bamboo."
}],
"id": "B",
"title": "Moderate",
"description": "Short description of category B."
},
{
"Product": [{
"id": "c01",
"name": "Ebony",
"description": "Short description of ebony."
},
{
"id": "c02",
"name": "Rosewood",
"description": "Short description of rosewood."
},
{
"id": "c03",
"name": "Bubinga",
"description": "Short description of bubinga."
}],
"id": "C",
"title": "Expensive",
"description": "Short description of category C."
}]
};
如果给定id(同样,在整个过程中没有嵌套的each()),我如何检索产品的名称和描述?我尝试了jQuery的find()和属性选择器([id=a03]),但没有成功。。。我可以精确定位节点的唯一方法是使用索引位置(例如,
var x=$(Test.Species.Category[2].Product[1].attr('description');
),但这不是目标
我正在沿着这些思路寻找更多的东西,但这似乎不起作用:
var x=$(Test.Species.Category[id='B'].Product[id='b02'].attr('description')代码>Javascript有一个函数filter(),可能会有帮助,但它在IE7或更低版本中不起作用。
var x = $(Test.Species.Category[0].Product[id='b02'].attr('description'));
var x = $(Test.Species.Category[0].Product[id='b02'].attr('name'));