Trying to parse JSON file with jQuery

前端 未结 2 606
孤独总比滥情好
孤独总比滥情好 2020-12-17 03:51

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

{
    \"students\": {
        \"student\": [
            {
                \"id         


        
2条回答
  •  自闭症患者
    2020-12-17 04:40

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

提交回复
热议问题