Okay so I'm given a large JSON Object from a GET request and it looks like this:
{
"partners": [
{
"firstName": "Mai",
"lastName": "Dost",
"email": "[email protected]",
"country": "United States",
"availableDates": [
"2017-05-29",
"2017-05-31",
"2017-06-01",
"2017-06-07",
"2017-06-08",
"2017-06-09",
"2017-06-11",
"2017-06-12"
]
},
{
"firstName": "Annamae",
"lastName": "Monty",
"email": "[email protected]",
"country": "United States",
"availableDates": [
"2017-05-31",
"2017-06-01",
"2017-06-07",
"2017-06-08",
"2017-06-09",
"2017-06-11",
"2017-06-12",
"2017-06-15",
"2017-06-16",
"2017-06-20"
]
},
I'm trying to scan through all the individual partners in this object but I can't seem to get anything returned other than "undefined". I'm using a for loop to try go into each value and I've asked it to alert me each time so I can see it's contents. Can you guys see what I'm doing wrong?
The JavaScript code for this is:
$.getJSON("someURL_IcantDisclose", function (result) {
var data = JSON.stringify(result, null, 2);
document.getElementById('load').innerHTML = data;
for(var i=0; i<data.partners.length;i++){
alert(data.partners[i]);
}
});
stringifyand then try to access the string as if it was an object?result) is an object with properties which happen to be arrays. The only "real" JSON involved in your example is stored indata(and somewhere in$.getJSON()but you can't work with that directly).