Using to jQuery to access JSON nested array
I had written a JSON file and corresponding jQuery to iterate through it. I've recently decided that I should probably read in one big JSON file, rather than several smaller ones. So my previous working code looked roughly like this:
JSON
- { "men" : [
- { "id":0,
- "name":"alex",
- "age":"25"
- },
- { "id":1,
- "name":"bob",
- "age":"26"
- },
- { "id":2,
- "name":"charlie",
- "age":"27"
- }
- ]}
And the jQuery I was using to iterate through it looked like this:
- //get the data from JSON file
- $.getJSON(url, function(data) {
- //populate the radio buttons
- $(data.men).each(function() {
- //do stuff
- });
- });
So now to combine two JSON files into one, I figured I needed to do this:
- {"people": [
- { "men" : [
- { "id":0,
- "name":"alex",
- "age":"25"
- },
- { "id":1,
- "name":"bob",
- "age":"26"
- },
- { "id":2,
- "name":"charlie",
- "age":"27"
- }
- ]},
- { "women" : [
- { "id":0,
- "name":"alexys",
- "age":"25"
- },
- { "id":1,
- "name":"bethany",
- "age":"26"
- },
- { "id":2,
- "name":"charlotte",
- "age":"27"
- }
- ]}
- ]}
But after much Googling and StackOverflow lurking, and many different attempts; I can not figure out how to access the array objects.
Any help would be greatly appreciated.