我正在試圖解析具有如下所示的確切結構的JSON文件。嘗試使用jQuery解析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"
}
]
}
}
我使用下面的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>');
});
});
}
有什麼我做錯了嗎?
錯在何處? – 2011-02-23 22:07:31
它應該是'$ .each(data.students.student,...'。這是否有幫助?如果沒有,那麼檢索數據可能會有問題。您是從服務器還是外部讀取文件? – 2011-02-23 22:09:09
data.students? – 2011-02-23 22:10:27