-1

I have an assignment for my current web development course, my issue is that I cannot see why I need to use JSON.parse() (it's a requirement though). I can guess where it would be used but with experimentation have not managed to render any results.

The general idea is we are drawing information from a JSON array in a separate file (on the same server obviously). This is done through a function contained in an ajax.js file written by the lecturer which is relatively straight forward as follows:

function getJSON(url, callbackFunction) {

//var request = new XMLHttpRequest();
var request = new XMLHttpRequest();

// Work around for browser caching problem that affects some browsers. It prevents
// the browser from using a cached copy that was stored by the browser from another visit.

if (url.indexOf('?') < 0) {
    today = new Date();// Add a unique query string to the end of the url 
                       // representing the current time to force the browser to fetch the data
                       // rather than using a locally cached copy that the browser fetched some earlier time. 
    url += '?' + today.getTime();
}

request.onreadystatechange = function (){

    if (request.readyState === 4 && request.status===200) {
        alert("Response Status = " + request.status); // You can safely comment out this line 
        alert("response = " + request.response);      // You can safely comment out this line 
        alert("response headers =  " + request.getAllResponseHeaders()); // You can safely comment out this line
        callbackFunction(request.response);
    }
};

try {
    request.open("POST", url);
    request.responseType ="json";
    request.send();
} 
catch ( exception ) {
    alert(exception);
}

}

It uses an XMLHTTPRequest function.

The callback function in this case is as follows at present:

function displayAllGames(results) {
    alert('ready to start work');

    var gamesArray = results.games;
    var myHTML = '<table id="table1" border="1">';

    for (var counter = 0; counter < gamesArray.length; counter++) {
        var game = gamesArray[counter];
        var venue = game.venue;
        var round = game.round;
        var date = new Date(game.date);
        var homeTeam = game.homeTeam.name;
        var awayTeam = game.awayTeam.name;

        myHTML += '<tr>';
        myHTML += '<td>' + round + '</td>';
        myHTML += '<td>' + venue + '</td>';
        myHTML += '<td>' + date.toLocaleDateString() + '</td>';
        myHTML += '<td>' + homeTeam + '</td>';
        myHTML += '<td>' + awayTeam + '</td>';
        myHTML += '</tr>';
    }

    myHTML += '</table>';

    var targetElement = document.getElementById('happyPlace');
    targetElement.innerHTML = myHTML;
}

When adding code for a JSON.parse() even as an extra at the end of the document nothing happens. The code as follows:

var parsedData = JSON.parse(gamesArray);
alert(parsedData[1]);

However no alert comes up and even when trying to write the single item to the end of the document with document.write nothing occurs. Can someone please explain what I'm doing wrong? It's driving me nuts.

JSON Content:

{
"games": [
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 14:20:00",
        "venue": "Maryborough",
        "homeTeam":
                {
                    "name": "Maryborough",
                    "goals": 11,
                    "points": 9
                },
        "awayTeam":
                {
                    "name": "Castlemaine",
                    "goals": 24,
                    "points": 10
                }
    },
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 14:20:00",
        "venue": "Eaglehawk",
        "homeTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 18,
                    "points": 19
                },
        "awayTeam":
                {
                    "name": "South Bendigo",
                    "goals": 3,
                    "points": 7
                }
    },
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 10,
                    "points": 5
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 19,
                    "points": 11
                }
    },
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 14:20:00",
        "venue": "Golden Square",
        "homeTeam":
                {
                    "name": "Golden Square",
                    "goals": 16,
                    "points": 10
                },
        "awayTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 9,
                    "points": 10
                }
    },
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 17:30:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "Sandhurst",
                    "goals": 10,
                    "points": 9
                },
        "awayTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 11,
                    "points": 16
                }
    },
    {
        "round": 2,
        "date": "Sat, 11 Apr 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 25,
                    "points": 22
                },
        "awayTeam":
                {
                    "name": "Maryborough",
                    "goals": 7,
                    "points": 3
                }
    },
    {
        "round": 2,
        "date": "Sat, 11 Apr 2015 14:20:00",
        "venue": "Kennington",
        "homeTeam":
                {
                    "name": "South Bendigo",
                    "goals": 10,
                    "points": 6
                },
        "awayTeam":
                {
                    "name": "Golden Square",
                    "goals": 15,
                    "points": 18
                }
    },
    {
        "round": 2,
        "date": "Sat, 11 Apr 2015 14:20:00",
        "venue": "Kyneton",
        "homeTeam":
                {
                    "name": "Kyneton",
                    "goals": 15,
                    "points": 11
                },
        "awayTeam":
                {
                    "name": "Sandhurst",
                    "goals": 22,
                    "points": 30
                }
    },
    {
        "round": 2,
        "date": "Sat, 11 Apr 2015 17:30:00",
        "venue": "Kangaroo Flat",
        "homeTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 12,
                    "points": 12
                },
        "awayTeam":
                {
                    "name": "Castlemaine",
                    "goals": 11,
                    "points": 10
                }
    },
    {
        "round": 2,
        "date": "Sun, 12 Apr 2015 14:20:00",
        "venue": "Strathfieldsaye",
        "homeTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 16,
                    "points": 16
                },
        "awayTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 11,
                    "points": 7
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Maryborough",
        "homeTeam":
                {
                    "name": "Maryborough",
                    "goals": 12,
                    "points": 14
                },
        "awayTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 14,
                    "points": 13
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Castlemaine",
        "homeTeam":
                {
                    "name": "Castlemaine",
                    "goals": 12,
                    "points": 13
                },
        "awayTeam":
                {
                    "name": "Gisborne",
                    "goals": 10,
                    "points": 10
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Eaglehawk",
        "homeTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 20,
                    "points": 21
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 10,
                    "points": 8
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Golden Square",
        "homeTeam":
                {
                    "name": "Golden Square",
                    "goals": 6,
                    "points": 8
                },
        "awayTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 13,
                    "points": 13
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "Sandhurst",
                    "goals": 20,
                    "points": 17
                },
        "awayTeam":
                {
                    "name": "South Bendigo",
                    "goals": 8,
                    "points": 3
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 14:20:00",
        "venue": "Maryborough",
        "homeTeam":
                {
                    "name": "Maryborough",
                    "goals": 9,
                    "points": 4
                },
        "awayTeam":
                {
                    "name": "Sandhurst",
                    "goals": 25,
                    "points": 26
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 14:20:00",
        "venue": "Eaglehawk",
        "homeTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 26,
                    "points": 14
                },
        "awayTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 7,
                    "points": 3
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 14:20:00",
        "venue": "Strathfieldsaye",
        "homeTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 10,
                    "points": 15
                },
        "awayTeam":
                {
                    "name": "Castlemaine",
                    "goals": 3,
                    "points": 6
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 9,
                    "points": 13
                },
        "awayTeam":
                {
                    "name": "Golden Square",
                    "goals": 11,
                    "points": 10
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 17:30:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "South Bendigo",
                    "goals": 6,
                    "points": 11
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 10,
                    "points": 11
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 14:20:00",
        "venue": "Castlemaine",
        "homeTeam":
                {
                    "name": "Castlemaine",
                    "goals": 10,
                    "points": 8
                },
        "awayTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 9,
                    "points": 15
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 14:20:00",
        "venue": "Strathfieldsaye",
        "homeTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 24,
                    "points": 24
                },
        "awayTeam":
                {
                    "name": "Maryborough",
                    "goals": 5,
                    "points": 2
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 14:20:00",
        "venue": "Golden Square",
        "homeTeam":
                {
                    "name": "Golden Square",
                    "goals": 17,
                    "points": 10
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 7,
                    "points": 6
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 14:20:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "South Bendigo",
                    "goals": 12,
                    "points": 19
                },
        "awayTeam":
                {
                    "name": "Gisborne",
                    "goals": 4,
                    "points": 6
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 17:30:00",
        "venue": "Kangaroo Flat",
        "homeTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 7,
                    "points": 8
                },
        "awayTeam":
                {
                    "name": "Sandhurst",
                    "goals": 17,
                    "points": 24
                }
    },
    {
        "round": 6,
        "date": "Sat, 9 May 2015 14:20:00",
        "venue": "Maryborough",
        "homeTeam":
                {
                    "name": "Maryborough",
                    "goals": 6,
                    "points": 6
                },
        "awayTeam":
                {
                    "name": "Golden Square",
                    "goals": 27,
                    "points": 26
                }
    },
    {
        "round": 6,
        "date": "Sat, 9 May 2015 14:20:00",
        "venue": "Kangaroo Flat",
        "homeTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 11,
                    "points": 9
                },
        "awayTeam":
                {
                    "name": "South Bendigo",
                    "goals": 8,
                    "points": 11
                }
    },
    {
        "round": 6,
        "date": "Sat, 9 May 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 9,
                    "points": 4
                },
        "awayTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 19,
                    "points": 17
                }
    },
    {
        "round": 6,
        "date": "Sat, 9 May 2015 14:20:00",
        "venue": "Kyneton",
        "homeTeam":
                {
                    "name": "Kyneton",
                    "goals": 5,
                    "points": 8
                },
        "awayTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 20,
                    "points": 18
                }
    },
    {
        "round": 6,
        "date": "Sun, 10 May 2015 14:20:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "Sandhurst",
                    "goals": 16,
                    "points": 17
                },
        "awayTeam":
                {
                    "name": "Castlemaine",
                    "goals": 3,
                    "points": 7
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Kangaroo Flat",
        "homeTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 9,
                    "points": 12
                },
        "awayTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 13,
                    "points": 11
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Castlemaine",
        "homeTeam":
                {
                    "name": "Castlemaine",
                    "goals": 18,
                    "points": 9
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 21,
                    "points": 13
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Eaglehawk",
        "homeTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 8,
                    "points": 11
                },
        "awayTeam":
                {
                    "name": "Golden Square",
                    "goals": 10,
                    "points": 14
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 1,
                    "points": 4
                },
        "awayTeam":
                {
                    "name": "Sandhurst",
                    "goals": 17,
                    "points": 25
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "South Bendigo",
                    "goals": 25,
                    "points": 18
                },
        "awayTeam":
                {
                    "name": "Maryborough",
                    "goals": 5,
                    "points": 6
                }
    }

}
Rory Perry
  • 97
  • 1
  • 10
  • JSON.parse turns a JSON **string** into a JavaScript **object**. They look similar but the type is important – Liam May 18 '15 at 13:10
  • What type is games array? If you [debug your js](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) is games array a string (`""`) or an object. I don't think you need to parse it because the getJson will turn the json into an object for you. – Liam May 18 '15 at 13:13
  • 1
    pls post the getJSON code –  May 18 '15 at 13:14
  • I understand it turns into an object, but then why can I not read an item out of the array position once it has been converted to an object? I still don't understand in anyway why I should need to use it when I can read and utilise the data from the JSON Array perfectly fine without it. – Rory Perry May 18 '15 at 13:14
  • You assign to `gamesArray` in your callback. If you've added the `JSON.parse`/`alert` at the end of your document, it's probably either `undefined` or an empty object at the time of invoking it, as it's after your callback (but will be run before the callback has been called). Check your browser console - I'm betting it's errored when trying to parse whatever `gamesArray` happens to be at the time. – James Thorpe May 18 '15 at 13:15
  • ....you don't need to use JSON.parse. I'm not clear why you think you do, but you don't – Liam May 18 '15 at 13:15
  • 1
    Its specified as a requirement of the assignment, as stated in the original question. – Rory Perry May 18 '15 at 13:17
  • 1
    you probably need `gameArray.data[counter]` - can you show us the actual JSON content? – Hogan May 18 '15 at 13:18
  • 1
    You should ask your lecturer _why_ it's required, since the function he's provided seems that it's already transforming the response from a string to objects for you (`responseType = 'json'`). – James Thorpe May 18 '15 at 13:21
  • I'd love to ask him, he's a part time lecturer and only works the day before lectures and the actual day of the lectures. Therein doesn't do anything work related on his days off. I'd rather have most of this sorted before I got to his lecture this coming Thursday. – Rory Perry May 18 '15 at 13:24
  • 1
    There appears there's no technical reason for it here though, so he's the only one who can realistically answer. If the response you were getting was a plain string, you'd need to pass `results` to it at the start of your callback... but it's not. – James Thorpe May 18 '15 at 13:26

1 Answers1

0

From what I can understand you should use JSON.parse if the results is not an object, but a string variable holding a valid JSON. This way you could use the Javacript object and do whatever you like.

cs04iz1
  • 1,737
  • 1
  • 17
  • 30