0

I wanted to use jQuery's $.each() and it works, but not in IE6. So enter json2. So I am getting the data and binding it to a var using

var theData = JSON.stringify(data);

Then calling through an array

    var i;
    for(i=0; i<theData.Event.Football.length; i++)
    {
        alert(theData.Event.Football[i].time);
    }

Although it's just saying "Cannot read property 'Football' of undefined"

Here is the JSON, after 6 hours of trying variations, i am use im just misunderstanding something simple.

{
"Event":{
    "Football":[
        {
          "title": "Some Event",
          "time": "6:00" ,
          "competitors": {
            "competitors1": "Boaty Mc Boat",
            "competitors2": "Disco Dave"
          },
          "win": {
            "win1": 1.3,
            "win2": 1.89,
            "win3": 1.79
          }
        }, {
          "title": "Some Event",
          "time": "7:00" ,
          "competitors": {
            "competitors1": "Flesh Wound",
            "competitors2": "None Shall Pass"
          },
          "win": {
            "win1": 2.03,
            "win2": 1.79,
            "win3": 1.79
         }
     ]
  }
}

Final Fix, there was a problem with the AJAX syntax, here is the working version that allows the above code to work

$.ajax({
    type: 'GET',
    url: "football1.json",
    dataType: "json",
    processData: true,
    contentType: "text/json; charset=utf-8",
    data: {},
    success: function(data){
        alert('working');
       // do stuff
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.debug(textStatus, errorThrown);
    }
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
Paddy
  • 772
  • 2
  • 11
  • 28
  • 3
    *"I wanted to use Jquery's $.each() and it works, but not in i.e 6. So enter json2."* Um...they have nothing to do with one another. – T.J. Crowder Aug 16 '16 at 07:26

2 Answers2

4

This line:

var theData = JSON.stringify(data);

takes a data structure and turns it into a string (JSON).

If data is already not a string, just use it directly.

If data is a string and you want to go the other way, you want

var theData = JSON.parse(data);

...where, again, data is a string containing JSON.

At which point, either $.each or your for loop (or your various other options, modulo IE6 (!) support) will loop through theData.Event.Football.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hey T.J Thank you for clearing it up. It works calling it directly, i should have mentioned the JSON data is called through an AJAX request and the loop is called in the .success() , however this doesn't work in IE 6,7 need to spin up another VM to test 8, but im guessing same story. – Paddy Aug 16 '16 at 07:57
  • So Long story short, i looked up some old versions AJAX requests, turns out it was just the syntax wasn't working. Thanks for pointing me in the right direction :D – Paddy Aug 16 '16 at 08:25
1

If you stringify the data, it will be a string. You cannot access the object's properties in a JSON-stringified representation of the object.

Your for loop looks fine and should work with IE6, but it should be executed on data, not theData.

Or is data also a string? Did you mean to use theData = JSON.parse(data)?

David Hedlund
  • 128,221
  • 31
  • 203
  • 222