Skip to main content
 首页 » 编程设计

python之使用 python (foursquare API) 解析 json 文件中的较低级别

2025年05月04日11pander-it

因为我刚开始使用 python,所以我认为我还没有很好地掌握解析 json 响应的概念,并且当我尝试只打印出 json 文件的某些部分时,我一直遇到同样的问题.在下面的代码中,我使用 foursquare checkins API 端点返回我的 checkin 历史记录(为简洁起见省略了身份验证过程):

    from rauth import OAuth2Service 
    import json 
    import pprint 
 
    fs_checkins = session.get(endpoint, params = query_params) 
    fs_checkin_data = json.loads(fs_checkins.content) 
 
    pprint.pprint(fs_checkin_data) 

这会产生如下所示的 json 响应:

    {u'response': {u'checkins': {u'count': 74, 
                                 u'items': [{u'photos': {u'count': 0, 
                                                         u'items': []}, 
                                             u'posts': {u'count': 0, 
                                                        u'textCount': 0}, 
                                             u'source': {u'name': u'foursquare for iPhone', 
                                                         u'url': u'https://foursquare.com/download/#/iphone'}, 
                                             u'timeZoneOffset': -240, 
                                             u'type': u'checkin', 
                                             u'venue': {u'beenHere': {u'count': 1, 
                                                                      u'marked': False}, 
                                                        u'canonicalUrl': u'https://foursquare.com/v/nitehawk-cinema/4da491f6593f8eec9a257e35', 
                                                        u'categories': [{u'icon': {u'prefix': u'https://foursquare.com/img/categories_v2/arts_entertainment/movietheater_', 
                                                                                   u'suffix': u'.png'}, 
                                                                         u'id': u'4bf58dd8d48988d17f941735', 
                                                                         u'name': u'Movie Theater', 
                                                                         u'pluralName': u'Movie Theaters', 
                                                                         u'primary': True, 
                                                                         u'shortName': u'Movie Theater'}], 
                                                        u'contact': {u'formattedPhone': u'(718) 384-3980', 
                                                                     u'phone': u'7183843980'}, 
                                                        u'id': u'4da491f6593f8eec9a257e35', 
                                                        u'like': False, 
                                                        u'likes': {u'count': 114, 
                                                                   u'groups': [{u'count': 114, 
                                                                                u'items': [], 
                                                                                u'type': u'others'}], 
                                                                   u'summary': u'114 likes'}, 
                                                        u'location': {u'address': u'136 Metropolitan Ave.', 
                                                                      u'cc': u'US', 
                                                                      u'city': u'Brooklyn', 
                                                                      u'country': u'United States', 
                                                                      u'crossStreet': u'btwn Berry St. & Wythe Ave.', 
                                                                      u'lat': 40.716219932353624, 
                                                                      u'lng': -73.96228637176877, 
                                                                      u'postalCode': u'11211', 
                                                                      u'state': u'NY'}, 
                                                        u'name': u'Nitehawk Cinema', 
                                                        u'stats': {u'checkinsCount': 11566, 
                                                                   u'tipCount': 99, 
                                                                   u'usersCount': 6003}, 
                                                        u'url': u'http://www.nitehawkcinema.com', 
                                                        u'venuePage': {u'id': u'49722288'}, 
                                                        u'verified': True}}]}}} 

我只想解析出嵌套在 'venue' 下的 'canonicalUrl' 和 'name' 并理解其结构所以:

    response 
       checkins 
          items 
             venue 
                canonicalUrl                  
                name 

我已经尝试通过 fs_checkin_data['response']['checkins'] 循环将 'items' blob 附加到一个空列表中:

    items = [] 
 
    for item in fs_checkin_data['response']['checkins']: 
        info = {} 
        info['items'] = item['items'] 
        items.append(info) 

然后我想我可以通过 that 空列表进行循环,将 'venue' blob 附加到另一个空列表中,最后只能打印出'canonicalUrl' 和 'name'(对于我即兴创作的丑陋的拼凑逻辑表示歉意,因为我不知道还有其他方法可以达到相同的结果)。

然而,上面的代码导致了这个错误:

          info['items'] = item['items'] 
    TypeError: string indices must be integers 

什么时候我不明白了

   for item in fs_checkin_data['response']['checkins']:  
       pprint.pprint(item) 

json 文件的那部分没有问题。

我知道必须有更好的方法来做到这一点,但我似乎无法找到一个简单、有效的解决方案,因此将不胜感激任何帮助。谢谢。

请您参考如下方法:

您需要遍历 items:

for item in fs_checkin_data['response']['checkins']['items']: 
    venue = item['venue'] 
    print venue['canonicalUrl'], venue['name'] 

checkins 本身仍然是一个只有两个键的字典,items 和 count。遍历 checkins 字典会迭代键,因此在您的代码中 item 设置为 'count',然后是 'items' (反之亦然)。另一方面,items 是一个字典列表,因此遍历该列表可以访问每个单独的项目。