无法使用python从json响应中获取特定对象

时间:2020-06-06 02:17:54

标签: python json foursquare

我将以下内容作为从json响应中提取一些信息的功能的一部分(在底部可以找到它的一部分),并且工作正常。但是...

venues_list=[]

url = 'https://api.foursquare.com/v2/venues/search?client_id={}&client_secret={}&ll={},{}&v={}&categoryId={}&radius={}&limit={}'.format(CLIENT_ID, CLIENT_SECRET, lat, lng, VERSION, search_categoryId, radius, LIMIT)

results = requests.get(url).json()["response"]['venues']

venues_list.append([(
            v['name'], 
            v['location']['lat'], 
            v['location']['lng'],  
            v['categories'][0]['name'],
            v['id']) for v in results])


要混合从json文件中检索的对象,我想添加:

v ['location'] ['postalCode']

但是不起作用。我收到 KeyError:'postalCode'

如果我这样做:

v ['位置'] ['距离']

v ['location'] ['country']

v ['location'] ['formattedAddress']

有效。我没有错误。

这些也不起作用:

v ['location'] ['cc']

v ['位置'] ['城市']

我得到相同的 KeyError:'cc' KeyError:'city'

有什么我想念的吗?您能帮我理解为什么会这样吗?

[{'id': '4ad4c061f964a52099f720e3',
  'name': 'Live Organic Food Bar',
  'location': {'address': '264 Dupont Street',
   'lat': 43.67505287052667,
   'lng': -79.40671518307245,
   'labeledLatLngs': [{'label': 'display',
     'lat': 43.67505287052667,
     'lng': -79.40671518307245}],
   'distance': 273,
   'postalCode': 'M5R 1V7',
   'cc': 'CA',
   'city': 'Toronto',
   'state': 'ON',
   'country': 'Canada',
   'formattedAddress': ['264 Dupont Street', 'Toronto ON M5R 1V7', 'Canada']},
  'categories': [{'id': '4bf58dd8d48988d1d3941735',
    'name': 'Vegetarian / Vegan Restaurant',
    'pluralName': 'Vegetarian / Vegan Restaurants',
    'shortName': 'Vegetarian / Vegan',
    'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/vegetarian_',
     'suffix': '.png'},
    'primary': True}],
  'referralId': 'v-1591407049',
  'hasPerk': False},

0 个答案:

没有答案