2012-01-23 9 views
0
class FourSquareTestHandler(SecurePageHandler): 
    def get(self): 
    logging.info('***********************') 
    logging.info('*****GET****') 
    logging.info('***********************') 
    try: 
     request =\ 
        urllib2.Request('https://api.foursquare.com/v2/users/self/checkins?oauth_token=LAF1W3VMSI0DGQYIBTNIYVIZG4VE2RFGESM45LDJRSQHFTK3&v=20120123') 
     data = simplejson.load(urllib2.urlopen(request)) 
     logging.info('=========================') 
     logging.info('*********Success*********') 
     logging.info('=========================') 
     logging.info(data) 
     logging.info('======================================') 


    except Exception: 
     logging.info('**********EXCEPTION*********') 
     pass 

    context = {} 
    return self.render_response('foursquaretest.html', **context) 

こんにちは、私はこのコードを書いてユーザーのチェックイン数を得ました。私は、私のためにシンプルなデシリアライズされたデータオブジェクトを取得することができます。私は現在、ええと、このpythonでfoursquareから与えられたユーザーIDのユーザーチェックインを取得する方法

{ 
    u 'notifications': [{ 
     u 'item': { 
      u 'unreadCount': 0 
     }, 
     u 'type': u 'notificationTray' 
    }], 
    u 'meta': { 
     u 'code': 200 
    }, 
    u 'response': { 
     u 'checkins': { 
      u 'count': 6, 
      u 'items': [{ 
       u 'venue': { 
        u 'verified': False, 
        u 'name': u 'ODEL Warehouse', 
        u 'contact': {}, 
        u 'location': { 
         u 'city': u 'Colombo', 
         u 'country': u 'Sri Lanka', 
         u 'postalCode': u '00600', 
         u 'state': u 'Western Province', 
         u 'address': u 'Highlevel Rd', 
         u 'lat': 6.8754399960498391, 
         u 'lng': 79.881239696240982 
        }, 
        u 'stats': { 
         u 'tipCount': 2, 
         u 'checkinsCount': 76, 
         u 'usersCount': 25 
        }, 
        u 'id': u '4d217410b69c6dcbf1787995', 
        u 'categories': [] 
       }, 
       u 'comments': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'photos': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'timeZone': u 'Asia/Colombo', 
       u 'type': u 'checkin', 
       u 'id': u '4f1d1940e4b0d1a657597d73', 
       u 'createdAt': 1327307072 
      }, { 
       u 'venue': { 
        u 'verified': False, 
        u 'name': u 'Chesmi Consolidated', 
        u 'contact': {}, 
        u 'location': { 
         u 'lat': 6.8783062620154825, 
         u 'city': u 'colombo 5', 
         u 'lng': 79.879610192565167, 
         u 'country': u 'Sri Lanka', 
         u 'address': u '21, Siebel Avenue,' 
        }, 
        u 'stats': { 
         u 'tipCount': 0, 
         u 'checkinsCount': 2, 
         u 'usersCount': 2 
        }, 
        u 'id': u '4ed4cab6e5fa7b1a923b1e1e', 
        u 'categories': [] 
       }, 
       u 'comments': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'photos': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'timeZone': u 'Asia/Colombo', 
       u 'type': u 'checkin', 
       u 'id': u '4f1d1920e4b0e6badd487a53', 
       u 'createdAt': 1327307040 
      }, { 
       u 'venue': { 
        u 'verified': False, 
        u 'name': u 'Commercial Bank', 
        u 'contact': {}, 
        u 'location': { 
         u 'city': u 'Colombo 5', 
         u 'country': u 'Sri Lanka', 
         u 'postalCode': u '00500', 
         u 'state': u 'Western Province', 
         u 'address': u 'High Level Rd', 
         u 'lat': 6.8776848396168955, 
         u 'lng': 79.879610192565167 
        }, 
        u 'stats': { 
         u 'tipCount': 0, 
         u 'checkinsCount': 42, 
         u 'usersCount': 9 
        }, 
        u 'id': u '4c85bdb1d4e237047aaf8588', 
        u 'categories': [{ 
         u 'pluralName': u 'Banks', 
         u 'primary': True, 
         u 'name': u 'Bank', 
         u 'shortName': u 'Bank/Financial', 
         u 'id': u '4bf58dd8d48988d10a951735', 
         u 'icon': { 
          u 'prefix': u 'https://foursquare.com/img/categories/shops/financial_', 
          u 'name': u '.png', 
          u 'sizes': [32, 44, 64, 88, 256] 
         } 
        }] 
       }, 
       u 'comments': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'photos': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'timeZone': u 'Asia/Colombo', 
       u 'type': u 'checkin', 
       u 'id': u '4f1d18f5e4b0e6badd485953', 
       u 'createdAt': 1327306997 
      }, { 
       u 'venue': { 
        u 'verified': False, 
        u 'name': u 'Siebel Avenue Bus Stop 138', 
        u 'contact': {}, 
        u 'location': { 
         u 'lat': 6.8775550799999996, 
         u 'country': u 'Sri Lanka', 
         u 'lng': 79.879341159999996 
        }, 
        u 'stats': { 
         u 'tipCount': 3, 
         u 'checkinsCount': 76, 
         u 'usersCount': 15 
        }, 
        u 'id': u '4e5c53b345dd045aab4d7dfa', 
        u 'categories': [{ 
         u 'pluralName': u 'Bus Stations', 
         u 'primary': True, 
         u 'name': u 'Bus Station', 
         u 'shortName': u 'Bus Station', 
         u 'id': u '4bf58dd8d48988d1fe931735', 
         u 'icon': { 
          u 'prefix': u 'https://foursquare.com/img/categories/travel/busstation_', 
          u 'name': u '.png', 
          u 'sizes': [32, 44, 64, 88, 256] 
         } 
        }] 
       }, 
       u 'comments': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'photos': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'timeZone': u 'Asia/Colombo', 
       u 'type': u 'checkin', 
       u 'id': u '4f1d18e1e4b0e2eeed04eff2', 
       u 'createdAt': 1327306977 
      }, { 
       u 'venue': { 
        u 'verified': False, 
        u 'name': u 'Visni', 
        u 'contact': {}, 
        u 'location': { 
         u 'lat': 6.8785901069641113, 
         u 'country': u 'Sri Lanka', 
         u 'lng': 79.879791259765625 
        }, 
        u 'stats': { 
         u 'tipCount': 1, 
         u 'checkinsCount': 8, 
         u 'usersCount': 1 
        }, 
        u 'id': u '4eb75312f5b94bd85c9fa375', 
        u 'categories': [{ 
         u 'pluralName': u 'Tech Startups', 
         u 'primary': True, 
         u 'name': u 'Tech Startup', 
         u 'shortName': u 'Tech Startup', 
         u 'id': u '4bf58dd8d48988d125941735', 
         u 'icon': { 
          u 'prefix': u 'https://foursquare.com/img/categories/shops/technology_', 
          u 'name': u '.png', 
          u 'sizes': [32, 44, 64, 88, 256] 
         } 
        }] 
       }, 
       u 'comments': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'photos': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'timeZone': u 'Asia/Colombo', 
       u 'type': u 'checkin', 
       u 'id': u '4f1d18cde4b0850c85e124d1', 
       u 'createdAt': 1327306957 
      }, { 
       u 'venue': { 
        u 'verified': False, 
        u 'name': u 'Calcey Technologies', 
        u 'contact': { 
         u 'phone': u '0112827560', 
         u 'formattedPhone': u '011 2 827560' 
        }, 
        u 'location': { 
         u 'city': u 'Colombo 5', 
         u 'country': u 'Sri Lanka', 
         u 'postalCode': u '00500', 
         u 'state': u 'Sri Lanka', 
         u 'crossStreet': u 'Highlevel Rd', 
         u 'address': u '21 Siebel Ave', 
         u 'lat': 6.8782598000000004, 
         u 'lng': 79.879707733333333 
        }, 
        u 'stats': { 
         u 'tipCount': 0, 
         u 'checkinsCount': 324, 
         u 'usersCount': 14 
        }, 
        u 'id': u '4c43efd62d3ec9b60e3f2eae', 
        u 'categories': [{ 
         u 'pluralName': u 'Tech Startups', 
         u 'primary': True, 
         u 'name': u 'Tech Startup', 
         u 'shortName': u 'Tech Startup', 
         u 'id': u '4bf58dd8d48988d125941735', 
         u 'icon': { 
          u 'prefix': u 'https://foursquare.com/img/categories/shops/technology_', 
          u 'name': u '.png', 
          u 'sizes': [32, 44, 64, 88, 256] 
         } 
        }] 
       }, 
       u 'comments': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'photos': { 
        u 'count': 0, 
        u 'items': [] 
       }, 
       u 'timeZone': u 'Asia/Colombo', 
       u 'type': u 'checkin', 
       u 'id': u '4f1d18a3e4b0a6271ab6c175', 
       u 'createdAt': 1327306915 
      }] 
     } 
    } 
} 
+0

@Hugo、この編集は質問を大きく変えます。質問者は、このような形式の質問を書くことができるかどうかこの質問をしなかった)。 質問を編集するのではなく、その書式を別の回答として入力する必要があります。 – 0xc0de

答えて

3

あなたdataのようなPythonの辞書オブジェクトを何かをされてきているため、データ・オブジェクト内の個々の要素にアクセスする方法を知りたいです。

ネストされた辞書とリストが内部にあります。例えば用

>>> data['notifications'] 
[{u'item': {u'unreadCount': 0}, u'type': u'notificationTray'}] 

>>> data['notifications']['item']['unreadCount'] 
0 

>>> data['response']['checkins']['count'] 
6 

>>> data['response']['checkins']['items'][0] 
    {u'comments': {u'count': 0, u'items': []}, 
    u'createdAt': 1327307072, 
    u'id': u'4f1d1940e4b0d1a657597d73', 
    u'photos': {u'count': 0, u'items': []}, 
    u'timeZone': u'Asia/Colombo', 
    u'type': u'checkin', 
    u'venue': {u'categories': [], 
       u'contact': {}, 
       u'id': u'4d217410b69c6dcbf1787995', 
       u'location': {u'address': u'Highlevel Rd', 
           u'city': u'Colombo', 
          u'country': u'Sri Lanka', 
           u'lat': 6.8754399960498391, 
           u'lng': 79.881239696240982, 
           u'postalCode': u'00600', 
           u'state': u'Western Province'}, 
       u'name': u'ODEL Warehouse', 
       u'stats': {u'checkinsCount': 76, 
          u'tipCount': 2, 
          u'usersCount': 25}, 
       u'verified': False}} 

これは、図6(チェックイン)エントリが存在示します。だから、

>>> data['response']['checkins']['items'][0]['comments']['items'][0] 

や他の分野についても同様、最初のコメント(その内容)を取得するには最初(0番目のリスト項目)のidチェックイン、

>>> data['response']['checkins']['items'][0]['id'] 
u'4f1d1940e4b0d1a657597d73' 

を取得します。

関連する問題