2012-03-13 181 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); 
});