2011-12-18 72 views
1

我想使用jquery解析這個json,並且每個都有一個循環。我只看到json的例子,它用一個標題(如結果)包裝在數組中。我如何解析像下面的json ...?如何使用.each解析json數組而不使用外部名稱?

 
$.each(json.results, function() { 
    console.log(this['title']); 
}); 
 
[{"title":"About Us","url_title":"about_us","entry_id":"155","channel_id":"2","author_id":"1","status":"open","entry_date":"1287508538","edit_date":"20101209122240","expiration_date":"0","about_body":"We are the greatest.","about_image":"","about_staff_title":"Joe","about_extended":"More about us.","color":"d12626"},{"title":"Contact Us","url_title":"contact_us","entry_id":"223","channel_id":"2","author_id":"1","status":"open","entry_date":"1291918929","edit_date":"20101209122310","expiration_date":"0","about_body":"Email us.","about_image":"","about_staff_title":"Bob","about_extended":"","color":"080480"}] 
+0

您的預期結果是什麼? – zizozu 2011-12-18 05:18:28

+2

喜歡這個? http://jsfiddle.net/TuhHt/ – 2011-12-18 05:21:53

+0

謝謝賈裏德,工作完美! – 2011-12-18 05:25:46

回答

3
 
$.each(json, function() {  
    console.log(this.title); 
}); 
+0

@digitalbart - 這與我的評論基本相同。 – 2011-12-18 05:28:30

+1

@JaredFarrish你想發表一個答案,我會接受它 – 2011-12-18 05:30:36

+0

@digitalbart - 不,沒關係。我們同時發佈了它。 – 2011-12-18 05:31:37

1

它不應該僅僅是$.each(json, function() ...)?你的JSON是一個數組,不包裝,所以直接傳遞給$.each()

+0

我不知道你通過直接傳遞給$ .each()?你能否詳細說明 – 2011-12-18 05:27:24

+0

@Sudhir和@Jared都顯示了什麼 - 如果你的JSON是一個數組,並且將它加載到一個名爲'json'的變量中,只需將'json'作爲第一個參數傳遞給'$ .each',而不是傳遞'json.results'。 – nrabinowitz 2011-12-18 05:29:59

+0

謝謝,我認爲這是有道理的。 – 2011-12-18 05:32:07