I am creating a website to practice my coding on and I came across a problem while doing so. I am trying to import data from a json file with a forloop and the variables came back as undefined. I think it has to do with the i variable in the foreach, but I could be wrong. Any help is much appreciated. Thanks!
<script>
$.getJSON('package.json', function(data){
for(var i in data)
{
var username = i.username;
var value = i.value;
var tokens= i.tokens;
$(".list-group").append('<li>' + username + ' has deposited $' + value + ' in ' + tokens + ' tokens</li>');
}
});
</script>
And here is a copy of the json file
{
"u1":
{
"username": "Username1",
"tokens": 2,
"value": 26
},
"u2":
{
"username": "Username2",
"tokens": 4,
"value": 292
},
"u3":
{
"username": "Username3",
"tokens": 10,
"value": 127
},
"u4":
{
"username": "Username4",
"tokens": 3,
"value": 12
}
}