Create contact table from JSON data

前端 未结 2 1821
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 08:32

I started a demonstration project on HTML, JSON and jQuery recently. Thing I want to achieve now is get data from the JSON file and load it into my table. I am new to JSON so it

相关标签:
2条回答
  • 2021-01-21 09:09

    With reference to this post

    JSON.parse unexpected character error

    I come to know that you parse already parsed JSON

    You can replace your script as follows:

        window.onload = function () {
           var contacts;
           $.getJSON('contact.json',function(contacts)
            {
              $(contacts.info).each(function(index, element){  
                   $('#contacts').append('<tr><td>' + element.fullname + '</td><td>'
                     + element.email + '</td><td>'
                     + element.phone + '</td><td>'
                     + element.badgeid + '</td></tr>');       
               })  
            });
         };
    
    0 讨论(0)
  • 2021-01-21 09:12

    Run your JSON through a JSON validator, for instance JSONLint.com. You have a syntax error in your JSON:

    {
        "length": 2,
        "info": [
            {
                "fullname":"Noob Here",
                "email":"myemail@server.com",
                "phone":"123456",
                "badgeid": "11111",  <---- Do not put a comma before a curly brace
            },
            {
                "fullname":"Newbie There",
                "email":"hisemail@server.com",
                "phone":"589433",
                "badgeid": "11112",  <---- remove comma before curly brace
            }
        ]
    }
    

    Your JSON should instead look like this:

    {
        "length": 2,
        "info": [
            {
                "fullname":"Noob Here",
                "email":"myemail@server.com",
                "phone":"123456",
                "badgeid": "11111"
            },
            {
                "fullname":"Newbie There",
                "email":"hisemail@server.com",
                "phone":"589433",
                "badgeid": "11112"
            }
        ]
    }
    
    0 讨论(0)
提交回复
热议问题