2

I have a function hitting a JSON file, and I am trying to get the url of the photos out of the JSON file but I can't seem to drill down far enough into the file to get them?

Here is the function:

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);
            }
        } 
    });

};

Here is what the JSON looks like, or the part I am focusing on, i am trying to get the photos.items[1] which are the photos with WxH = 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"
          }
        },

2 Answers 2

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);
                });
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0
results.response.checkins.items[i].photos.items[i];

It seems wrong. Why those two indexes are both i? They may be 0 and i or i and j(nested loop).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.