2012-01-30 69 views
0
o = { 
    "photos": { 
     "page": 1, 
     "pages": 46, 
     "perpage": 5, 
     "total": "230", 
     "photo": [{ 
      "id": "6643924777", 
      "owner": "[email protected]", 
      "secret": "3b7c2f6469", 
      "server": "7153", 
      "farm": 8, 
      "title": "Huevos rellenos de bonito del norte Serrats", 
      "ispublic": 1, 
      "isfriend": 0, 
      "isfamily": 0}, 
     { 
      "id": "6371789575", 
      "owner": "[email protected]", 
      "secret": "dda25093e0", 
      "server": "6105", 
      "farm": 7, 
      "title": "ALBÓNDIGAS DE CARNE", 
      "ispublic": 1, 
      "isfriend": 0, 
      "isfamily": 0}, 
     { 
      "id": "6200723807", 
      "owner": "[email protected]", 
      "secret": "bf4fc5c84f", 
      "server": "6159", 
      "farm": 7, 
      "title": "albóndigas en salsa", 
      "ispublic": 1, 
      "isfriend": 0, 
      "isfamily": 0}, 
     { 
      "id": "62", 
      "owner": "[email protected]", 
      "secret": "def72cc124", 
      "server": "6128", 
      "farm": 7, 
      "title": "albóndigas en salsa", 
      "ispublic": 1, 
      "isfriend": 0, 
      "isfamily": 0}, 
     { 
      "id": "6171324677", 
      "owner": "[email protected]", 
      "secret": "9f7a7489f5", 
      "server": "6161", 
      "farm": 7, 
      "title": "Pastel de patata con bonito del norte de Conservas Serrats y bechamel", 
      "ispublic": 1, 
      "isfriend": 0, 
      "isfamily": 0}]} 

} 

這是一個查詢 我特林這樣檢索該JSON數組值

for (var key in o) { 
    for (var key1 in o[key]) { 
     var current = o[key][key1].id; 
     console.log(key1); //returns the pages information 
     console.log(current); //returns null 
    }  
} 

基本上我需要構造與這種格式

<img src="http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg" /> 

回答

5
for (var i = 0; i < o.photos.photo.length; i++) { 
    var photo = o.photos.photo[i]; 

    var img = document.createElement('img'); 
    var src = 'http://farm' + photo.farm + '.staticflickr.com/' + 
       photo.server + '/' + photo.id + '_' + photo.secret + '.jpg'; 
    img.setAttribute('src', src); 

    // TODO: do something with the img DOM element we just created 
} 

,這裏是一個live demo

2

圖像的Flickr的API JSON結果嘗試

jQuery.getJSON() - 從服務器載入JSON編碼的數據usi一個GET HTTP請求。

jQuery.parseJSON - 取得一個結構良好的JSON字符串,並返回結果的JavaScript對象