简体   繁体   中英

Accessing JSON nested data

I'm trying to access the values node in the following JSON document using something like this (assume data is the JSON document):

console.log(data.values);

Is this because the document is one big array?

[
  {
     "key": "Failed",
     "vis": "1",
     "values": [
        {
           "date": "2014-02-26T05:00:00.000Z",
           "total": 0.07142857142857142
        }
     ]
  },
  {
     "key": "Pass",
     "vis": "1",
     "values": [
        {
           "date": "2014-02-26T05:00:00.000Z",
           "total": 1.325781
        }
     ]
  },
]

Because you're dealing with a nested array structure, you'd have to do something like below to extract and aggregate the values into a more accessible collection

var objects = [
  {
     "key": "Failed",
     "vis": "1",
     "values": [
        {
           "date": "2014-02-26T05:00:00.000Z",
           "total": 0.07142857142857142
        }
     ]
  },
  {
     "key": "Pass",
     "vis": "1",
     "values": [
        {
           "date": "2014-02-26T05:00:00.000Z",
           "total": 1.325781
        }
     ]
  },
];

var aggregates = [];

objects.forEach(function(o, i){
   o.values.forEach(function(v, i){
      aggregates.push(v);
   });
});

console.log(aggregates);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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