I have the following JSON file (rectangles.json):
{
"rectangles":[
{
"x":0,
"y":0,
"width":20,
"height":10,
"color":"red"
},
{
"x":25,
"y":0,
"width":20,
"height":10,
"color":"red"
}
]
}
I get the following error: 'Unexpected Token :' on line 2. I can't figure out the issue. Any suggestions?
Below is the javascript I have in a local file:
function load(){
var myData = JSON.parse(rectangles);
var can = document.getElementById('rectangleCanvas');
var context = can.getContext('2d');
for (i=0; i<myData.length; i++){
context.fillStyle = myData[i].color;
context.fillRect(myData[i].x, myData[i].y, myData[i].width, myData[i].height);
}
}
And her is my HTML from another local file:
<!DOCTYPE html>
<html>
<head>
<title>Fetch JSON array Data</title>
<script type="text/javascript" src="myScript.js"></script>
<script type="text/javascript" src="rectangles.json"></script>
</head>
<body>
<canvas id="rectangleCanvas" width="22528" height="20922"></canvas>
<script>load();</script>
</body>
</html>