在json中搜索特定键并找到上一个和下一个类似的键

时间:2014-04-10 05:01:20

标签: javascript jquery json

与brnwdrng的问题类似,我正在寻找一种方法来搜索类似JSON的对象,并找到最后一个类似的密钥和下一个密钥。 假设我的对象的结构是这样的:

TestObj = {
"Categories": [{
    "Products": [{
        "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."
}]

};

现在我想找到一个名为" oak"在我发现我之前想要找到最后一个标题之前" oak"在这种情况下是" maple"和下一个标题是" bamboo"。

如果有人能指出正确的方向或给我一个假代码,我将感激不尽。

由于

2 个答案:

答案 0 :(得分:0)

做这样的事情

i = 0;
foreach obj in TestObj
   if obj.title = Oak,
      last_title = TestObj[i].title;
      next_title = TestObj[i+2].title;
      i++;
endfor

答案 1 :(得分:0)

好的,这是一个快速的代码。

TestObj = {
"Categories": [{
    "Products": [{
        "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."
},
{
    "Products": [{
        "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."
}]
};

var oak, beforeOak, afterOak;

for(var i=0;i<TestObj.Categories.length;i++){
    var products = TestObj.Categories[i].Products;
       for(var j=0; j< products.length;j++){
        var product = products[j];

        if(product.name === 'Oak'){
            oak = product;
            beforeOak = products[j-1];
            afterOak = products[j+1];
            break;

        }
    }
    if(oak) break;
}

alert("Oak : "+JSON.stringify(oak));
alert("Before Oak : "+JSON.stringify(beforeOak));
alert("After Oak :" + JSON.stringify(afterOak));

在此处查看此操作: http://jsfiddle.net/ejDk8/4/

这很容易理解。如果你还没有得到它,JSON是JavaScript的原生对象结构。因此,您可以使用dot表示法访问它。

Starter on JSON in JavaScript

Btw ,你的json有点不一致。阵列的关键&#34;产品&#34;在第二类 是&#34;产品&#34;而不是&#34;产品&#34;。所以我冒昧地纠正它。