用jQuery解析JSON文件?

时间:2012-03-27 13:23:14

标签: jquery json

我有一个功能命中JSON文件,我试图从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],这些是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 个答案:

答案 0 :(得分: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);
                });
            }
        });
    });
});

答案 1 :(得分:0)

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

似乎错了。为什么这两个索引都是i?它们可能是0iij(嵌套循环)。