2013-01-04 29 views
0

我的JSON響應是這樣的{ "items":[{"s": "2","f": "host","m": "hi..:)"}, ] }如何用jquery讀取json值?

我寫了下面的代碼來讀取JSON數據。但沒有獲得任何價值。警示框也不起作用。如果任何人請告訴我在哪裏,我沒有錯......

$.ajax({ 

    url: "ChatHandler.aspx?action=chatheartbeat", 
    cache: false, 
    dataType: "json", 
    success: function (data) { 

     $.each(data.items, function (i, item) { 

      if (item) { // fix strange ie bug 

       chatboxtitle = item.f; 

       alert(item.m); 

      } 
     }); 
}); 

回答

1

嘗試這樣的:

$.each(data, function(i, item) { 
    alert(item[0].m); 
});​ 

FIDDLE

+0

不工作..... – user1240045

2

應該

alert(data.items[i].m) 

fiddle這裏

+0

不工作... ... – user1240045

+0

你得到什麼錯誤? – bipen

+0

我沒有收到任何error.i使用螢火蟲檢查json的數據。以下輸出我得到{「項目」:[{「s」:「2」,「f」:「主機」,「米」 :「嗨.. :)」},]} – user1240045

0
$.each(data.items, function(i, item) { 
    if (item) { 
     alert(i);//alerts 0 - the index of the first item 
     var s = item.s 
     var f = item.f 
     var m = item.m; 
     alert(f);// alerts "host" 
    } 
}); 

的「怪蟲,即」是由於這樣的事實,你有一個空的對象(看到最後一個逗號數組:)這第二項是「不確定」,在您的數據集