读取JSON数据并循环遍历它

时间:2016-08-12 18:04:06

标签: javascript json ajax

我有3个文件

  1. index.html
  2. data.json
  3. app.js
  4. 在我的WAMP服务器上

    。我希望遍历JSON数据并更新HTML文件的内容。

    这些是我文件的内容:

    data.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();
    

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

2 个答案:

答案 0 :(得分:2)

您正在遇到此问题,因为返回的对象是一对key: value

您需要访问作为数组的user属性。

您的代码

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

解析json字符串后得到的值是key的javascript对象:&#39; user&#39;和value:这是一个数组

原来是

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++) {

答案 1 :(得分:1)

看一下你对象的结构:

{
  "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='';
              }}
          }}