2011-05-06 130 views
0

我從webservice獲取以下JSON字符串。jquery,遍歷json數組

[{"TITLE":"asdasdasd","DESCRIPTION":"asdasd","PORTFOLIOID":1}, 
{"TITLE":"sss","DESCRIPTION":"sss","PORTFOLIOID":2}, 
{"TITLE":"sdfsdf","DESCRIPTION":"sdfsfsdf","PORTFOLIOID":3}] 

我可以在jQuery中循環這個數組並輸出單個的鍵/值對嗎?

+4

而答案是:是的,你可以! – 2011-05-06 13:21:17

+0

偉大,關心讓我知道如何? – twsJames 2011-05-06 13:21:51

+0

簡單的問題,簡單的回答:) – Jeff 2011-05-06 13:22:26

回答

1

絕對。假設你告訴jQuery來評估該響應,JSON與AJAX方法,你只需做到這一點:

<script> 
$(data).each(function(idx, obj) //this loops the array 
{ 
    $(obj).each(function(key, value) //this loops the attributes of the object 
    { 
     console.log(key + ": " + value); 
    } 
} 
</script> 
+0

謝謝,作品一個款待 – twsJames 2011-05-06 13:48:44

+0

@twsJames沒問題(: – 2011-05-06 13:53:06

2
var a = [{"TITLE":"asdasdasd","DESCRIPTION":"asdasd","PORTFOLIOID":1}, ....] 

$(a).each(function(index) 
{ 
    //this is the object in the array, index is the index of the object in the array 
    alert(this.TITLE + ' ' this.DESCRIPTION) 
}); 

退房jQuery的文檔的更多信息... http://api.jquery.com/jQuery.each/

+0

謝謝,作品一個款待 – twsJames 2011-05-06 13:49:07