2013-03-13 3 views
0

JSON에서 연관 배열을 출력하는 PHP 코드가 있습니다. 배열의 객체에 어떻게 액세스 할 수 있습니까? 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)를 수행하고 데이터 객체를 붙여 넣습니다. 기회는 당신이 생각하는 것이 아닙니다. –

+0

두 번째 배열에 어떻게 액세스 할 수 있는지 알고 있습니까? – sammyukavi

+0

숫자를 키로 사용할 수 없습니다. 대신 "item"을 키로 사용하여 단어를 사용하고 숫자를 포함하는 각 항목 객체에 "index"키를 만듭니다. – Terry

답변

0

이것은 내가 찾고 있었던 것입니다.

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