weixin_33709364 2018-01-17 11:03 采纳率: 0%
浏览 22

列表中填充了JSON文件

I'm trying to get a list filled with strings from a JSON file. It work's for one string 'name', but when i try to add another string 'age' it gets an undefined error.

(part of) HTML file

<ul id="people"></ul>

      var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        var response = JSON.parse(xhttp.responseText);
        var people = response.people;

        var output = '';
        for(var i = 0;i < people.length;i++){
          output += '<li>'+people[i].name+'</li>';
        }
        document.getElementById('people').innerHTML = output;
      }
  };
  xhttp.open("GET", "people.json", true);
  xhttp.send();

//json file //

  {
  "highscore": [
    {
      "name":"Billy",
      "age": 12,
      "date": 11 "november" 2017,
      "wins": 4
    },
    {
      "name":"Willem",
      "age": 16,
      "date": 14 "juni" 2017,
      "wins":2

    },
    {
      "name":"Sara",
      "age": 14,
      "date": 18 "mei" 2017,
      "wins": 1
    },
    {
      "name":"Jeff",
      "age": 11,
      "date": 2 "april" 2017,
      "wins":1
    }
  ]
}

enter image description here

  • 写回答

1条回答 默认 最新

  • weixin_33739523 2018-01-17 11:15
    关注

    Your json file has a weird(and probably wrong) kind of value:

    "date": 11 "november" 2017,

    I don't know if you can create a json file with a value like this.

    But you can do it like:

      peoples =[
        {
          "name":"Billy",
          "age": 12,
          "date": '11 november 2017',
          "wins": 4
        },
        {
          "name":"Willem",
          "age": 16,
          "date": '14 juni 2017',
          "wins":2
    
        },
        {
          "name":"Sara",
          "age": 14,
          "date": '18 mei 2017',
          "wins": 1
        },
        {
          "name":"Jeff",
          "age": 11,
          "date": '2 april 2017',
          "wins":1
        }
      ]
    
     
    
            var people = peoples
    
            var output = '';
            for(var i = 0;i < people.length;i++){
              output += '<li>'+people[i].name + ':' +people[i].age+'</li>';
            }
            document.getElementById('people').innerHTML = output;
          
    <ul id="people"></ul>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)