2013-03-13 70 views
0

我有一個php代碼,它在json中輸出關聯數組。我如何訪問數組中的對象。我知道訪問json數組中的第一項,我將不得不把data.item0.adlink,但我想遍歷data對象,並輸出json數組中的每個對象的所有內容。我的數組和代碼如下。謝謝。Jquery PHP JSON輸出關聯數組

陣列

{"item0":{"image":"preview.jpg","file":"video.mp4","adImage":"none","adLink":"http:\/\/sammyukavi.com\/"},"item1":{"image":"preview.jpg","file":"video.mp4","adImage":"none","adLink":"http:\/\/sammyukavi.com\/"},"item2":{"image":"preview.jpg","file":"video.mp4","adImage":"none","adLink":"http:\/\/sammyukavi.com\/"}} 

代碼

$.ajax({ 
         type: "GET", 
         dataType: "json", 
         data: {'first':'1','last':'3'} , 
         beforeSend: function(x) { 
          if(x && x.overrideMimeType) { 
           x.overrideMimeType("application/json;charset=UTF-8"); 
          } 
         }, 
         url: 'test.php', 
         success: function(data) { 
//I want to get a single item from the data object 
          $.each(data,function(){ 
//output a property for the single item here 
console.log($(this)) 
          }) 
         } 
        }) 
       }; 
+0

做console.error(data)並粘貼你的數據對象。機會不是你想象的那樣。 – 2013-03-13 13:36:21

+0

你知道我如何訪問第二個數組嗎? – sammyukavi 2013-03-13 13:39:04

+0

您不能將數字用作關鍵字。相反,使用一個單詞,說「項目」作爲關鍵,然後在每個包含該數字的項目對象中創建一個「索引」鍵。 – Terry 2013-03-13 13:46:25

回答

0

這就是我一直在尋找。

function(data) { 
         $.each(data, function(index, value) { 
          console.log(value) 
         }) 
        }