I want to use JS to read from an external JSON file, which is an array of n objects, and n is big. So, preferably, I want to write the array with each object taking a line. However, I found it only worked when everything is put in one single line; any line break messed things up. I wonder why. Below, I use a simplified example to illustrate. Odd!
The data.json file that worked:
data = '[{"name": "Ashwin","age": "20"},{"name": "Abhinandan","age": "21"}]';
The data.json file that did not work:
data = '[{"name": "Ashwin","age": "20"},
{"name": "Abhinandan","age": "21"}]';
The HTML file with JavaScript:
<script type="text/javascript" src="data.json"></script>
<script>
var mydata = JSON.parse(data);
alert(mydata[0].name);
alert(mydata[0].age);
alert(mydata[1].name);
alert(mydata[1].age);
</script>