I am new in javascript. I am having problem with simple json file reading. Here is the sample code.
function readJson() {
$.getJSON('./resources/json/comments_type.json', function(data) {
$.each(data, function(index, comment) {
tempList.push(comment);
});
});
for(var i = 0 ; i<tempList.length;i++)
{
console.log(tempList[i].text);
}
}
Here I was trying to iterate the tempList array after reading from the JSON file. But the console.log is not showing anything. But if I try console.log(tempList)
it works. tempList
is a global variable. I am calling the readJson
function from another function. The JSON file saved here JSON file