4

I am trying to parse a JSON file with the exact stucture as in the following.

{
    "students": {
        "student": [
            {
                "id": 1,
                "name": "John Doe",
                "image": "pic1.jpg",
                "homepage": "http: //www.google.com"
            },
            {
                "id": 2,
                "name": "Jane Doe",
                "image": "pic1.jpg",
                "homepage": "http: //www.google.com"
            }
        ]
    }
}

I am using the following jQuery function:

function GetStudents(filename)
{
    $.getJSON(filename, function(data){
        $.each(data.student, function(i,s){
            var id = s.id;;
            var name = s.name;;
            var img = s.image;;
            var homepage = s.homepage;
            $('.networkTable').append('<tr><td><img src="' + img + '" class="picEven pic" width="33" height="35"></td><td><a href="'+ homepage + '" class="networkLink">' + name + '</a></td></tr>');
        });
    });
}

Is there something I am doing wrong?

5
  • 4
    It should be $.each(data.students.student,.... Does this help? If not then there might be a problem retrieving the data. Are you reading the file from your server or an external one? Commented Feb 23, 2011 at 22:09
  • 2
    looks like it should be $.each(data.students.student, /*other code here...*/); Commented Feb 23, 2011 at 22:10
  • Oh yeah, I missed that point by Felix. That could be the true reason. Commented Feb 23, 2011 at 22:11
  • That solved the issue :D. Thank you very much, i struggled with this the entire day :( Commented Feb 23, 2011 at 22:16
  • @Felix: You should post your answer. Commented Feb 23, 2011 at 22:18

2 Answers 2

6

You are not accessing the correct element. data does not point to students, it points to the outer most element {students:...} (students is an property of it). The array is contained in data.students.student:

$.each(data.students.student, function() {
    //...
});

Further notes:

  • You don't need to create a local variable if you access a property only once (but of course it might be more readable).

  • While having consecutive semicolons ;; is not wrong, it is unnecessary and confusing (at least it confuses me ;))

Sign up to request clarification or add additional context in comments.

Comments

1
s.nametry: <br/>
$("document").ready(function() {
    $.getJSON(fileUrl,

    function(data)
    {
        $("#div-my-table").text("&lt;table&gt;");
        $.each(data, function(i, item) {
            $("#div-my-table").append("&lt;tr&gt;&lt;td&gt;" + item.prop1 +"&lt;/td&gt;&lt;td&gt;" + item.prop2 + "&lt;/td&gt;&lt;/tr&gt;");
        });
        $("#div-my-table").append("&lt;/table>");
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.