我JSON數據(增加了可讀性換行)返回解析和訪問JSON數據
[{"pk": 7, "model": "pycourt_login.orders", "fields": {"status": 0, "delivered": false, "order_id": "6count1%2", "student_id": 5, "counterid": "counter1", "datetime": "2011-04-05 01:44:01", "dish": 6, "transaction_id": "2008010661301935441", "quantity": 2}},
{"pk": 8, "model": "pycourt_login.orders", "fields": {"status": 0, "delivered": false, "order_id": "9count1%1", "student_id": 5, "counterid": "counter1", "datetime": "2011-04-05 01:44:14", "dish": 9, "transaction_id": "2008010661301935454", "quantity": 1}}]
如何在jQuery中訪問的每個項目。
我試圖做這樣的事情(假定/ pendingorders返回JSON)
$(document).ready(function(){
jQuery.ajax({
cache: false,
url: "/pendingorders",
type: "GET",
success: function(json) {
$.each(json.results, function(i,dish){
alert(dish.pk);
},'json')
},
});
});
這是我在Firebug得到錯誤:
object is undefined
[Break On This Error] length = object.length,
jquery.js (line 605)
感謝您提供豐富的答案。我認爲「.results」是一些使其可迭代的方法。 – 2011-04-05 03:55:13
如果我們可以做json劫持,我想知道當我在django中序列化數據庫查詢的結果時爲什麼會得到這樣的結果。他們應該爲此做點事情。 – 2011-04-05 03:58:00