如何遍历JSON中深层嵌套的对象?

时间:2016-07-01 06:45:52

标签: javascript json loops nested

我知道有很多关于迭代JSON对象的问题,但是我找不到与我的确切问题相关的问题。这是我试图迭代的JSON:

psinsights = {
 "kind": "pagespeedonline#result",
 "id": "/speed/pagespeed",
 "responseCode": 200,
 "title": "PageSpeed Home",
 "score": 90,
 "pageStats": {
  "numberResources": 22,
  "numberHosts": 7,
  "totalRequestBytes": "2761",
  "numberStaticResources": 16,
  "htmlResponseBytes": "91981",
  "cssResponseBytes": "37728",
  "imageResponseBytes": "13909",
  "javascriptResponseBytes": "247214",
  "otherResponseBytes": "8804",
  "numberJsResources": 6,
  "numberCssResources": 2
 },
 "formattedResults": {
  "locale": "en_US",
  "ruleResults": {
    "AvoidBadRequests": {
      "localizedRuleName": "Avoid bad requests",
      "ruleImpact": 0.0
    },
    "MinifyJavaScript": {
      "localizedRuleName": "Minify JavaScript",
      "ruleImpact": 0.1417,
      "urlBlocks": [
      {
        "header": {
       "format": "Minifying the following JavaScript resources could reduce their size by $1 ($2% reduction).",
       "args": [
        {
         "type": "BYTES",
         "value": "1.3KiB"
        },
        {
         "type": "INT_LITERAL",
         "value": "0"
        }
       ]
        },
        "urls": [
        {
          "result": {
         "format": "Minifying $1 could save $2 ($3% reduction).",
         "args": [
          {
           "type": "URL",
           "value": "http://code.google.com/js/codesite_tail.pack.04102009.js"
          },
          {
           "type": "BYTES",
           "value": "717B"
          },
          {
           "type": "INT_LITERAL",
           "value": "1"
          }
         ]
        }
       },
       {
        "result": {
         "format": "Minifying $1 could save $2 ($3% reduction).",
         "args": [
          {
           "type": "URL",
           "value": "http://www.gmodules.com/ig/proxy?url\u003dhttp%3A%2F%2Fjqueryjs.googlecode.com%2Ffiles%2Fjquery-1.2.6.min.js"
          },
          {
           "type": "BYTES",
           "value": "258B"
          },
          {
           "type": "INT_LITERAL",
           "value": "0"
          }
         ]
        }
       }
      ]
     }
    ]
   },
   "SpriteImages": {
    "localizedRuleName": "Combine images into CSS sprites",
    "ruleImpact": 0.0
   }
  }
 },
 "version": {
  "major": 1,
  "minor": 11
 }
};

现在,我正在尝试编写一个迭代所有ruleResults对象的函数,并返回一个localizedRuleName属性的数组。根据JSON,ruleResults有三个成员对象(AvoidBadRequests,MinifyJavaScript和SpriteImages)。其中每个都有一个我试图访问的localizedRuleName属性,但是当我打印出我的数组时,它是空白的。以下是我编写函数的方法:

function ruleList(results) {

    var ruleArray = [];

    for(var ruleName in results.formattedResults.ruleResults){

       ruleArray[counter] = results.formattedResults.ruleResults[ruleName].localizedRuleName;
    }

    return ruleArray;
}

console.log(ruleList(psinsights));

你们能帮助我走上正轨吗?我基本上使用相同的方法迭代JSON的pageStats并且它工作得很好。我不知道为什么我不能使用这些更深层次的嵌套对象和属性。

2 个答案:

答案 0 :(得分:1)

你的问题不是你的迭代,而是你未定义的变量" counter"。 而不是使用计数器可以使用" push"功能:

function ruleList(results) {

    var ruleArray = [];

    for(var ruleName in results.formattedResults.ruleResults){

       ruleArray.push(results.formattedResults.ruleResults[ruleName].localizedRuleName);
    }

    return ruleArray;
}

小提琴:https://jsfiddle.net/fo9h56gh/

希望这有帮助。

答案 1 :(得分:0)

你可能会收到一个javascript错误,因为没有定义计数器。你可以试试这个:

function ruleList(results) {

  var ruleArray = [];
  var counter = 0;

  for(var ruleName in results.formattedResults.ruleResults){

    ruleArray[counter] = results.formattedResults.ruleResults[ruleName].localizedRuleName;

    counter++;
  }

  return ruleArray;
}