简体   繁体   English

从json文件导入时,如何修复未定义对象数组的变量?

[英]How can I fix variables of an array of objects being undefined when being imported from a json file?

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. 我正在尝试使用forloop从json文件导入数据,并且变量返回为未定义。 I think it has to do with the i variable in the foreach, but I could be wrong. 我认为这与foreach中的i变量有关,但我可能是错的。 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 这是json文件的副本

{
  "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
  }
}

if data is an array, i will be an Integer-value string of the index . 如果data是一个数组,则i将是index的Integer值字符串。

You'll want data[i].username , data[i].value , data[i].tokens instead 您需要使用data[i].usernamedata[i].valuedata[i].tokens


if data is an object, you don't need to iterate through it 如果data是对象,则无需遍历数据

You'll want data.username , data.value , data.tokens instead 您将需要data.usernamedata.valuedata.tokens代替

if the data from json is array of object use it; 如果json中的数据是对象数组,请使用它;

    <script>
            $.getJSON('package.json', function(data){
                for(var i in data)
                {
                    var username = data[i].username;
                    var value = data[i].value;
                    var tokens= data[i].tokens;
                    $(".list-group").append('<li>' + username + ' has deposited $' + value + ' in ' + tokens + ' tokens</li>');
                }
            });
</script>

else use it 否则用它

 <script>
            $.getJSON('package.json', function(data){

                    var username = data.username;
                    var value = data.value;
                    var tokens= data.tokens;
                    $(".list-group").append('<li>' + username + ' has deposited $' + value + ' in ' + tokens + ' tokens</li>');

            });
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何阻止未定义的变量在邮递员上发送? - How do I block undefined variables from being sent on Postman? 如何修复从它生成的数组中翻转的 tilemap? - How can I fix my tilemap being flipped from the array it's generated from? 如果正在搜索值,如何在数组中返回 json 对象 - How can I return a json object in an array if the value is being searched for 如何防止对象在画布中模糊? - How can I prevent objects from being fuzzy in canvas? 动态加载脚本时,如何让 WebStorm 识别本地导入的文件? - How do I make WebStorm recognize a locally imported file when a script is being dynamically loaded? 从另一个文件导入的常量调用时不使用玩笑模拟 - Jest mock not being used when called from a constant being imported from another file 传递默认值时,如何解决“节点未定义”错误? - How to fix 'node is undefined' error when a default value is being passed? 在 Angular 中导入 protractor 时如何修复“浏览器”未定义 - How to fix 'browser' being undefined when importing protractor in Angular 从main.js导入canv时,如何解决“ Uncaught ReferenceError:canv未在otherfile.js:3上定义”的问题 - How Would one fix “Uncaught ReferenceError: canv is not defined at otherfile.js:3” when canv is being imported from main.js 如何解决“当尝试从json文件中获取未定义的url时”? - How to fix 'when trying to get a url from a json file i get undefined'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM