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
}
}
}