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

时间:2022-03-23 14:31:16

I know there are plenty of questions about iterating through JSON objects but I haven't found one that quite relates to my exact problem. This is the JSON that I'm trying to iterate through:

我知道有很多关于迭代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
 }
};

Now, I'm trying to write a function that iterates through all of the ruleResults objects and returns an array of the localizedRuleName properties. According to the JSON, ruleResults has three member objects (AvoidBadRequests, MinifyJavaScript, and SpriteImages). Each of these has a localizedRuleName property I'm trying to access, but when I print out my array, it's blank. Here's how I've written my function:

现在,我正在尝试编写一个迭代所有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));

Can you guys help me get on the right track? I used basically this same method to iterate through the pageStats of the JSON and it worked perfectly. I'm not sure why I can't get it to work with these deeper nested objects and properties.

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

2 个解决方案

#1


1  

your problem is not your iteration, but your undefined variable "counter". Instead of using a counter can use the "push" function:

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

function ruleList(results) {

    var ruleArray = [];

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

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

    return ruleArray;
}

fiddle: https://jsfiddle.net/fo9h56gh/

Hope this helps.

希望这可以帮助。

#2


0  

you're probably getting a javascript error since counter is not defined. you can try this:

你可能会收到一个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;
}

#1


1  

your problem is not your iteration, but your undefined variable "counter". Instead of using a counter can use the "push" function:

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

function ruleList(results) {

    var ruleArray = [];

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

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

    return ruleArray;
}

fiddle: https://jsfiddle.net/fo9h56gh/

Hope this helps.

希望这可以帮助。

#2


0  

you're probably getting a javascript error since counter is not defined. you can try this:

你可能会收到一个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;
}