试图用jQuery解析JSON文件

时间:2021-04-03 21:52:29

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

我试图解析具有精确结构的JSON文件,如下所示。

{
    "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:

我使用以下jQuery函数:

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?

有什么我做错了吗?

2 个解决方案

#1


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:

您没有访问正确的元素。数据并不指向学生,它指向最外层的元素{学生:...}(学生是其中的一个属性)。该数组包含在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 ;))

    连续分号时;没有错,这是不必要的和令人困惑的(至少它让我困惑;))

#2


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>");
    });
});

#1


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:

您没有访问正确的元素。数据并不指向学生,它指向最外层的元素{学生:...}(学生是其中的一个属性)。该数组包含在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 ;))

    连续分号时;没有错,这是不必要的和令人困惑的(至少它让我困惑;))

#2


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>");
    });
});