0

我有一个 JSON 输入

{
    "Categories": {
        "Facets": [{
            "count": 1,
            "entity": "Company",
            "Company": [{

                "entity": "Ford Motor Co",

                "Ford_Motor_Co": [{
                    "count": 1,
                    "entity": "Ford"
                }]
            }]
        }, {
            "count": 4,
            "entity": "Country",
            "Country": [{

                "entity": "Germany",
                "Germany": [{
                    "count": 1,
                    "entity": "Germany"
                }],
                "currency": "Euro (EUR)"
            }, {

                "entity": "Italy",
                "Italy": [{
                    "count": 1,
                    "entity": "Italy"
                }],
                "currency": "Euro (EUR)"
            }, {

                "entity": "Japan",
                "Japan": [{
                    "count": 1,
                    "entity": "Japan"
                }],
                "currency": "Yen (JPY)"
            }, {

                "entity": "South Korea",
                "South_Korea": [{
                    "count": 1,
                    "entity": "South Korea"
                }],
                "currency": "Won (KRW)"
            }]
        }, {
            "count": 5,
            "entity": "Persons",
            "Persons": [{
                "count": 2,
                "entity": "Dodge"
            }, {
                "count": 1,
                "entity": "Dodge Avenger"
            }, {
                "count": 1,
                "entity": "Major League"
            }, {
                "count": 1,
                "entity": "Sterling Heights"
            }]
        }]

    }
}

我需要将每个级别中实体的值放入数组中。

[Company, Ford Motor Co, Ford, ....... , Sterling Heights]

我能够通过代码的第一级

for (var k in h.Categories.Facets)
{

alert(h.Categories.Facets[k].entity);

}

我如何通过内层来获取实体的值?

4

4 回答 4

1

最一般的递归答案:

function getEntities(any) {
    var entities = [];
    if ('entity' in any​) {
        entities.push(any.entity);
    }
    for (var prop in any) {
        if (any[prop] instanceof Object && any.hasOwnProperty(prop)) {
            entities.append(getEntities(any[prop]));
        }
    }
    return entities;
}
console.log(getEntities(h));

该行:

 if (any[prop] instanceof Object && any.hasOwnProperty(prop)) { 

防止数字/空值被轰炸,并且 any.hasOwnProperty(prop) 弥补了喜欢附加到对象原型的框架。

于 2012-05-03T21:00:41.103 回答
1

您应该对每个实体执行一次 foreach 。如果你知道有多少层,你可以嵌套循环。如果不是 - 可能应该使用递归函数。

编辑

递归函数:

function getEntities(ent)
{
   alert(ent);
   for (var l in ent)
   {
      getEntities(ent[l].entity);
   }
}

然后使用:

for (var k in h.Categories.Facets)
{
   getEntities(h.Categories.Facets[k]);
}

祝你好运!

于 2012-05-03T20:49:33.527 回答
0

一种方法是编写一个接受数组并从中提取方法的递归方法。

于 2012-05-03T20:49:49.277 回答
0

如建议的那样,您可以使用递归函数来遍历每个可能的嵌套组合:

var allEntities = [];
function getEntities(obj)
{
   if (obj != null)
   {
       var entityName = obj["entity"];
       alert(entityName);
       if (entityName != null)
       {
           allEntities.push(entityName);
           var adjName = entityName.replace(/ /gi, "_");
           var entities = obj[adjName];
           if (entities != null)
           {
               for (var e in entities)
               {
                   var innerEntities = getEntities(entities[e]);
                   for (var inner in innerEntities)
                       allEntities.push(innerEntities[inner]);
               }
           }
       }
    }
}    

for (var k in h.Categories.Facets)
{
    alert(h.Categories.Facets[k].entity);
    getEntities(h.Categories.Facets[k]);
}
alert(allEntities.length);
于 2012-05-03T20:58:56.063 回答