简体   繁体   English

如何访问未命名数组json中的对象

[英]How to access the objects inside an unnamed array json

I have an json response from a link and when I call the $http.get function to get the data. 我有一个来自链接的json响应,当我调用$ http.get函数获取数据时。 The values which I'm trying to get are placed inside unnamed arrays. 我试图获取的值放在未命名的数组中。

The json response: json响应:

{
  "kind": "analytics#gaData",
  "rows": [
    [
      "Windows",
      "28514"
    ],
    [
      "Android",
      "6473"
    ],
    [
      "iOS",
      "4011"
    ],
    [
      "Macintosh",
      "846"
    ],
    [
      "Linux",
      "752"
    ]
  ]
}

I'm using angular functions $http.get but I don't know how to access those values Windows -> 28514 我正在使用角度函数$ http.get,但我不知道如何访问这些值Windows-> 28514

at the javascript momentally I'm just trying to get those objects before doing anything with them so I'm just passing them into console. 在使用javascript时,我只是想先获取这些对象,然后再对它们执行任何操作,所以我只是将它们传递到控制台中。

$http.get('https://radiopendimi-woosh.appspot.com/query?id=ahRzfnJhZGlvcGVuZGltaS13b29zaHIVCxIIQXBpUXVlcnkYgICAgPiJjAoM').then(function(data){
          console.log(data.rows);
      }); 

You have to loop through the array: 您必须遍历数组:

$http.get('https://radiopendimi-woosh.appspot.com/query?id=ahRzfnJhZGlvcGVuZGltaS13b29zaHIVCxIIQXBpUXVlcnkYgICAgPiJjAoM').then(function(data){
    for (var i = 0; i < data.rows.length; i++;) {
        console.log(data.rows[i]);
    }
});

You could convert the rows array into an object: 您可以将rows数组转换为一个对象:

var lookup = data.rows.reduce(function(out, pair) {
    out[pair[0]] = pair[1];
    return out;
}, {})

Then lookup will be something like this: 然后lookup将如下所示:

{
  "Windows": "28514",
  "Android": "6473",
  "iOS": "4011",
  "Macintosh": "846",
  "Linux": "752"
}

And you can access by name: 您可以按名称访问:

lookup.Windows
> 28514

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM