I need to access some JSON with JQuery. For some reason, it’s not working and since the getJSON method fails silently, I can’t figure out what’s wrong. I have checked that the JSON Url is correct. Thanks!
HTML:
<html>
<head>
<script src="//code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON('../src/json/baseball.json', function(data) {
$("#add").html(data.baseball[0].levels[0].games[0].versus);
});
});
</script>
</head>
<body>
<p id= "add"></p>
</body>
</html>
JSON:
{"baseball":
[{
"gender":"boys",
"levels":[
{
"level": "varsity",
"games":[
{
"versus":"Fullerton",
"homeaway":"Home",
"month":"February",
"date":"27",
"year":"2012",
"troyscore":"32",
"vsscore":"41",
},
{
"versus":"Sunny Hills",
"homeaway":"Away",
"month":"March",
"date":"28",
"year":"2012",
"troyscore":"20",
"vsscore":"17",
}]
},
{
"level": "jv",
"games":[
{
"versus":"Sonora",
"homeaway":"Home",
"month":"January",
"date":"20",
"year":"2012",
"troyscore":"15",
"vsscore":"21",
},
{
"versus":"Valencia",
"homeaway":"Away",
"month":"April",
"date":"30",
"year":"2012",
"troyscore":"40",
"vsscore":"1",
}]
}]
},
{
"gender":"girls",
"levels":[
{
"level": "varsity",
"games":[
{
"versus":"Acacia",
"homeaway":"Home",
"month":"February",
"date":"27",
"year":"2012",
"troyscore":"32",
"vsscore":"41",
},
{
"versus":"LV",
"homeaway":"Away",
"month":"March",
"date":"28",
"year":"2012",
"troyscore":"20",
"vsscore":"17",
}]
},
{
"level": "jv",
"games":[
{
"versus":"Commonwealth",
"homeaway":"Home",
"month":"January",
"date":"20",
"year":"2012",
"troyscore":"15",
"vsscore":"21",
},
{
"versus":"Xishan",
"homeaway":"Away",
"month":"April",
"date":"30",
"year":"2012",
"troyscore":"40",
"vsscore":"1",
}]
}]
}]
}
Each of the final properties of your inner objects have trailing commas, which makes your JSON badly formed. Just try pasting it into jsonlint.com. It will wine at every object’s last property.
Complaint:
$.getJSONwill fail silently if the returned JSON is not strictly well-formed. I want to provide a reference from the docs, but it appears that the jQuery site is down….bleh.