0

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]);
  }
});
2
  • 2
    Do you mean "JSON array" or "array"? The former is a string (JSON is a string format), the latter is an... array. You stringify and then try to access the string as if it was an object? Commented Sep 30, 2017 at 11:57
  • 1
    That's neither a JSON Array nor a JSON Object. That (result) is an object with properties which happen to be arrays. The only "real" JSON involved in your example is stored in data (and somewhere in $.getJSON() but you can't work with that directly). Commented Sep 30, 2017 at 11:57

2 Answers 2

4

You are attempting to loop over the strinigified version of the result - $.getJSON will return the parsed JSON so you can use result directly:

$.getJSON("someURL_IcantDisclose", function (result) {
  for(var i=0; i< result.partners.length; i++){
    alert(result.partners[i]);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

3

Not sure what these two lines are doing var data = JSON.stringify(result, null, 2);document.getElementById('load').innerHTML = data;, but the following snippet will allow you to loop over the partners

$.getJSON("someURL_IcantDisclose", function (result) {
  for(var i=0; i<result.partners.length; i++){
    alert(result.partners[i]);
  }
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.