2012-03-13 55 views
1

我有以下阿賈克斯你看到下面:AJAX從JSON.stringify(數據)解析

$.ajax({ 

    url: url, 

    type: 'POST', 

    dataType: 'json', 

    data: JSON.stringify(json), 

    contentType: 'application/json; charset=utf-8', 

    success: function (Data) { 



     alert(JSON.stringify(Data)); 



    }, 

    error: error 

}); 

當我做警報(JSON.stringify(數據));它顯示的數據如下所示。數據是我送回的對象。它回到3行4列。我如何迭代stringify中的數據,因爲我需要將其存儲到HTML表格中?

{"Data":[{"Iden":"12","Date":"01/23/2011","City":"Clearwater","State":"FL"},{"Iden":"19","Date":"02/09/2012","City":"Elgin","State":"IL"},{"Iden":"14","Date":"06/22/2010","City":"Newport Beach","State":"CA"}]} 

回答

2

你想使用jQuery的每個函數:

$.each(Data, function(index, element){ 
    alert("Index: " + index + ", Element: " + element); 
}); 
+0

當我嘗試這我得到索引爲0,1,2 ...和元素作爲每個字符....不知道我在做什麼錯... – ASD 2012-06-04 13:47:14

0

它傳遞給$。每個功能之前,必須先解析JSON數據

var d = JSON.parse(Data); 


You can also directly call like this too 

$.each(d.Data, function(index, element){ 
    alert("Index: " + index + ", Element: " + element); 
}); 

For ex if the json is like this 
{"note":{"to":"Tove","from":"Jani","heading":"Reminder","body":"Don't forget me this weekend!"}} 

$.each(d.note, function(index, element){ 
    alert("Index: " + index + ", Element: " + element); 
});