2012-03-27 88 views
2

我有一個功能打JSON文件,我試圖從JSON文件中獲取照片的URL,但我似乎無法深入鑽取文件得到它們?用jQuery解析JSON文件?

下面是函數:

var pics = []; 

function jsonData(){ 
    $.ajax({ 
    url: "https://api.foursquare.com/v2/users/self/checkins?oauth_token=FUKXDJRWIB0AQ2MQUKUEUSB3KW2TMYKUMFGYLYUHBBH14CQ0&v=20120126", 
    cache: false, 
    dataType: 'json', 
    success: function(results) { 
     var lat; 
     var long; 
     var paths = []; 
     for(var i = 0; i < results.response.checkins.items.length; i++) { 
      var pic = results.response.checkins.items[i].photos.items[0].sizes.items[0]; 
      pics.push(pic); 
      } 
     } 
    }); 

}; 

這裏是JSON看起來像什麼,或者我注重的部分,我試圖讓photos.items [1]這與寬x高的照片= 300:

{ 
    "meta": { 
    "code": 200 
    }, 
    "notifications": [ 
    { 
     "type": "notificationTray", 
     "item": {} 
    } 
    ], 
    "response": { 
    "checkins": { 
     "count": 1385, 
     "items": [ 
     { 
      "id": "4f71b513e4b0684643f7929e", 
      "createdAt": 1332851987, 
      "type": "checkin", 
      "shout": "Fish are still alive", 
      "timeZone": "America/New_York", 
      "timeZoneOffset": -240, 
      "venue": {}, 
      "photos": { 
      "count": 1, 
      "items": [ 
       { 
       "id": "4f71b515e4b0559c393d50dc", 
       "createdAt": 1332851989, 
       "url": "https://is1.4sqi.net/pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0.jpg", 
       "sizes": { 
        "count": 4, 
        "items": [ 
        { 
         "url": "https://is1.4sqi.net/pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0.jpg", 
         "width": 720, 
         "height": 537 
        }, 
        { 
         "url": "https://is0.4sqi.net/derived_pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0_300x300.jpg", 
         "width": 300, 
         "height": 300 
        }, 
        { 
         "url": "https://is0.4sqi.net/derived_pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0_100x100.jpg", 
         "width": 100, 
         "height": 100 
        }, 
        { 
         "url": "https://is0.4sqi.net/derived_pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0_36x36.jpg", 
         "width": 36, 
         "height": 36 
        } 
        ] 
       }, 
       "source": { 
        "name": "foursquare for iPhone", 
        "url": "https://foursquare.com/download/#/iphone" 
       }, 
       "user": { 
        "id": "43", 
        "firstName": "christian", 
        "lastName": "bovine", 
        "photo": "https://is1.4sqi.net/userpix_thumbs/AN3FGD1WOWXA4S2F.jpg", 
        "gender": "male", 
        "homeCity": "New York, NY", 
        "canonicalUrl": "https://foursquare.com/xtianbovine", 
        "relationship": "self" 
       }, 
       "visibility": "public" 
       } 
      ] 
      }, 
      "comments": { 
      "count": 0, 
      "items": [] 
      }, 
      "source": { 
      "name": "foursquare for iPhone", 
      "url": "https://foursquare.com/download/#/iphone" 
      } 
     }, 

回答

1
$(function() { 
    var pics = []; 
    var json_source = 'https://api.foursquare.com/v2/users/...'; 
    $.getJSON(json_source, function (results) { 
     $.each(results.response.checkins.items, function (i, item) { 
      if (item.photos.count > 0) { 
       $.each(item.photos.items, function (i, photo) { 
        pics.push(photo.url); 
       }); 
      } 
     }); 
    }); 
}); 
0
results.response.checkins.items[i].photos.items[i]; 

這似乎是錯誤的。爲什麼這兩個指數都是i?它們可能是0iij(嵌套循環)。