2011-08-18 50 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 
}