2011-08-18 51 views
0

有沒有一種方法可以通過使用索引值而不是密鑰來遍歷JSON結果? 我想查看json數據結果並獲取發送的鍵和值。通過JSONResult中的數據循環

例如

$.get('/Home/GetTypes', function (data) 
     { 
     $.each(function(index) { 
      //Just as an example I know this will not work. Thanks 
     '<a class="p-button" href="/Wizard/Create/" + data[index][value] + " '">' + data[index][key] + '</a>' 
     }  

     }); 
+1

什麼呢,你回來的樣子JSON字符串? –

回答

0

假設數據字典的數組,你接近:

$.each(data, function(index, obj) { 
     // then obj and data[index] both point to the nth entry in data 
+0

這將如何返回關鍵數據和數值數據?我會認爲data [index]只會返回值。 – Keith

+0

'index' *是關鍵!它會更清楚地寫爲'$ .each(data,function(key,value){'? – Malvolio

+0

呵呵。謝謝我認爲它傳遞的是數字索引,但是非常好。謝謝 – Keith

0
for (key in data) { 
    key = the key name 
    data[key] = the value 
}