I'm having trouble accessing child elements of a JSON file. Here is my JSON file:
var actualGameData = '[{
"gameBackgroundMusic": "background.wav",
"narratorCongratulations": "congratulations.wav",
"hitSound": [
"hit1.wav",
"hit2.wav",
"hit3.wav"
],
"narratorHitSound": [
"narratorhit1.wav",
"narratorhit2.wav",
"narratorhit3.wav"
],
"narratorMissSound": [
"narratormiss1.wav",
"narratormiss2.wav",
"narratormiss3.wav"
],
"levels": {
"Tumble in the Castle": {
"Time Limit": 60000,
"Background Image": {
"Object Name": "backgroundCastle",
"Image": "backgroundCastle.png"
},
"Foreground Image": {
"Object Name": "foregroundCastle",
"Image": "foregroundCastle.png"
},
"Targets": {
"Target1": {
"x axis": 45,
"y axis": 11
},
"Target2": {
"x axis": 545,
"y axis": 141
},
"Target3": {
"x axis": 495,
"y axis": 33
}
}
},
"Jungle Rumble": {
"Time Limit": 30000,
"Background Image": {
"Object Name": "backgroundJungle",
"Image": "backgroundJungle.png"
},
"Foreground Image": {
"Object Name": "foregroundJungle",
"Image": "foregroundJungle.png"
},
"Targets": {
"Target1": {
"x axis": 465,
"y axis": 141
},
"Target2": {
"x axis": 525,
"y axis": 111
},
"Target3": {
"x axis": 405,
"y axis": 63
}
}
}
}
}];
And I want to access all of the level names. What I want to see is 2 alerts showing: "Tumble in the castle" and "Jungle Rumble". I've tried the following, but it doesn't work. Any ideas? :) Many thanks.
<html>
<head></head>
<body>
<script type="text/javascript" language="javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript" language="javascript" src="game.json"></script>
<script>
$(document).ready(function() {
var obj = $.parseJSON(actualGameData);
$.each(obj, function() {
alert(this['levels.child']);
});
});
</script>
<span></span>
</body>
</html>