3

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 took me a day to get to nothing, the data was not loaded into my table.

Here is my JSON file, 'contact.json':

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

My script to load data:

window.onload = function () {
    var contacts;
    setTimeout(loadData(contacts, "contact"), 2000);
    $(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>');       
    })
};

function loadData(myobject, myfile){
    myobject = $.parseJSON("../data/" + myfile + ".json");
};

Please notice that I may want to load from various JSON file, so loadData have some agruments there. Otherwise, it will parse JSON file directly.

I already had a '#contact' table declared in HTML. The error console said:

Uncaught SyntaxError: Unexpected token.
jQuery.extend.parseJSONjquery.min.js:442
loadDataHomepage.html:23
window.onload

Why is this error appearing? How do I fix this problem?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shinigamae
  • 854
  • 3
  • 18
  • 35

2 Answers2

4

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>');       
           })  
        });
     };
Community
  • 1
  • 1
Usman
  • 3,200
  • 3
  • 28
  • 47
2

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"
        }
    ]
}
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • Thanks jmort253, I was so careless about the structure. But the error is still there after fixed JSON file. – Shinigamae Apr 10 '12 at 04:18
  • 1
    Try loading the JSON file in your browser using that same URL. Make sure you're not seeing a cached copy. Alternatively, add a query parameter to the end of the URL to bust the cache, like `$.parseJSON("../data/" + myfile + ".json?t=12345");`. Also, if you haven't done so, rerun your JSON through JSONLint until it validates without errors. Also, try clearing your cache. Doing all of those should ensure you're not seeing something cached in your browser. What URL is your page loaded at in reference to ../data directory. Make sure the path is correct. – jamesmortensen Apr 10 '12 at 04:36
  • 1
    Great, it works out after I cleaned all the cache. Seems it because I cached files using manifest and it kept loading the old one. Thank again @jmort253. – Shinigamae Apr 10 '12 at 07:13