Trying to parse JSON file with jQuery -


i trying parse json file exact stucture in 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 using 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 doing wrong?

you not accessing correct element. data not point students, points outer element {students:...} (students property of it). array contained in data.students.student:

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

further notes:

  • you don't need create local variable if access property once (but of course might more readable).

  • while having consecutive semicolons ;; not wrong, unnecessary , confusing (at least confuses me ;))


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -