2011-10-06 49 views
0

在jQuery中,我如何獲取DATA中的每個'itemName'並將其附加到posts-container?遍歷一個對象並追加到一個容器

所以我不得不HTML這樣.....

標題:哇剛剛看到這個

標題:測試

等....

感謝

Json

{ 
    "ERRORS": [], 
    "DATA": [ 
     { 
      "itemID": "012be513-9b2d-4ed6-9b84-4097a610a823", 
      "itemValidTo": "October, 29 2011 00:00:00", 
      "itemName": "wow just seen this", 
      "image": { 
       "imageCreated": "October, 06 2011 00:00:00", 
       "imageURL": "ha.jpg", 
       "imageID": "16fbdb40-8591-4a3b-b18e-bda494ac213f" 
      }, 
      "itemActive": true, 
      "createdBy": { 
       "UserID": "DA1F154B-EF46-49BD-18C84C49478AD48E", 
       "accNumber": "" 
      }, 
      "itemTxt": "SO impressed with this!", 
      "itemModified": "October, 06 2011 00:00:00" 
     }, 
     { 
      "itemID": "30d2f2c1-58ca-4b3d-b3e0-d284ae5b25ab", 
      "itemName": "test", 
      "image": { 
       "imageCreated": "October, 05 2011 00:00:00", 
       "imageURL": "image.jpg", 
       "imageID": "bc869a94-fee5-4fc8-bd21-e2de2f020310" 
      }, 
      "itemActive": true, 
      "createdBy": { 
       "UserID": "DA1F154B-EF46-49BD-18C84C49478AD48E" 
      }, 
      "itemTxt": "test", 
      "itemModified": "October, 05 2011 00:00:00" 
     } 
    ], 
    "MESSAGES": [ 
     { 
      "TOTAL": 2, 
      "CURRENTPAGE": 1, 
      "TOTALPAGES": 1 
     } 
    ] 
} 

腳本

<script type="text/javascript"> 
$(document).ready(function(){ 

    $.ajax({ 
     type: 'get', 
     url: 'http://do.com/api/id/123', 
     dataType: 'json', 
     data: '', 
     success: function(r){ 
      $('#waiting').hide(500); 
      var output = ''; 

      //loop here and add append I assume? 



      jQuery('#posts-container').append(output); 
     }, 
     error: function(XMLHttpRequest, textStatus, errorThrown){ 

     } 
    }); 

    return false; 
}); 

回答

1
$.each(r.DATA,function(key,value){ 
    alert(value.itemName) 
    alert(value.itemID) 
    . 
    . 
}) 

U可以從這裏http://jsfiddle.net/Uk5ZJ/

+0

這種檢查讓我不確定 – Niklas

+0

如果它給未定義的,還是你沒有得到的數據格式,你所寫的。它應該是另一種格式,我猜..因爲它適用於jsfiddle。我添加了鏈接,你可以檢查它 –

+0

它應該工作。如果沒有,你可以說什麼問題?你不明白的地方? –

相關問題