【问题标题】:Reading JSON data and looping through it读取 JSON 数据并循环遍历它
【发布时间】:2016-08-12 18:04:06
【问题描述】:

我有 3 个文件

  1. index.html
  2. data.json
  3. app.js

在我的 WAMP 服务器上。我希望遍历 JSON 数据并更新 HTML 文件的内容。

这些是我文件的内容:

数据.json

{
    "user": [{
        "goal": "HTML essential training",
        "type": "Beginner",
        "date": "20/07/2016"
    }, {
        "goal": "CSS essential training",
        "type": "Beginner",
        "date": "30/07/2016"
    }, {
        "goal": "CSS core concepts",
        "type": "Intermediate",
        "date": "10/08/2016"
    }, {
        "goal": "Javascript essential training",
        "type": "Beginner",
        "date": "20/08/2016"
    }, {
        "goal": "Object Oriented JS",
        "type": "Advanced",
        "date": "30/08/2016"
    }]
}

app.js

var request = new XMLHttpRequest();
request.open('GET', 'data.json');
request.onreadystatechange = function() {
    if ((request.readyState === 4) && (request.status === 200)) {
        var items = JSON.parse(request.responseText);
        console.log(items);

        for (var key in items) {
            console.log(key);
            var output = "<tr><td><input type='checkbox' name='record'></td><td>" + items[key].goal + "</td><td>" + items[key].type + "</td><td>" + items[key].date + "</td></tr>";
            console.log(output);
            $("table tbody").append(output);
            output = '';

        }

    }
}

request.send();

当我运行此代码时,会创建一行并将所有值设置为未定义。我认为我的循环逻辑有问题。请帮助我。

【问题讨论】:

    标签: javascript json ajax


    【解决方案1】:

    您遇到了这个问题,因为返回的对象是一个 key: value 对。

    您需要访问user 属性,它是一个数组。

    您的代码

    var items=JSON.parse(request.responseText);
    

    解析json字符串后得到的值是带有key:'user'和value:的javascript对象,这是一个数组

    应该是

    var data =JSON.parse(request.responseText);
    var items = data.user;   <--  this is an array of objects
    
    // iterate using a for loop, since it is not an object technically
    for(var i=0; i < items.length; i++) {
    

    【讨论】:

    • 感谢您的帮助!!
    • “你一直在用那个词。我不认为它意味着你认为它的意思。” There is no such thing as a "JSON Object".
    • @JasonCust 感谢您清除它。我实际上将它称为 json 字符串。会更新帖子。还有
    【解决方案2】:

    看看你的对象的结构:

    {
      "user": [
        {
          "goal": "HTML essential training",
          "type": "Beginner",
          "date": "20\/07\/2016"
        },
        {
          "goal": "CSS essential training",
          "type": "Beginner",
          "date": "30\/07\/2016"
        },
        {
          "goal": "CSS core concepts",
          "type": "Intermediate",
          "date": "10\/08\/2016"
        },
        {
          "goal": "Javascript essential training",
          "type": "Beginner",
          "date": "20\/08\/2016"
        },
        {
          "goal": "Object Oriented JS",
          "type": "Advanced",
          "date": "30\/08\/2016"
        }
      ]
    }
    

    只有一个键,值是一个数组。

    你需要这样的循环:

              for (var key in items){if (items.hasOwnProperty(key)) {
                  for (var i in items[key]) {if (items[key].hasOwnProperty(i)){
                    console.log(items[key][i]);
                    var output="<tr><td><input type='checkbox' name='record'></td><td>" + items[key][i].goal + "</td><td>" + items[key][i].type +  "</td><td>" + items[key][i].date + "</td></tr>";
                    console.log(output);
                    $("table tbody").append(output);
                    output='';
                  }}
              }}
    

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2015-09-21
      • 1970-01-01
      相关资源
      最近更新 更多