为了获取一些信息,我使用Python和requests库调用API。到目前一切尚好。代码运行良好,响应成功:
请求:
def countries():
# Application header
header = {
'Authorization': '123456789',
'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
}
# Make the request and return the country code
request_url = requests.get(
'https://endpoint/api/Countries', headers=header)
countries_dictionary = request_url.json()
countries()我将响应存储到变量中,并将其编码到JSON中。当我打印变量的值时,结果如下:
代码:
print(countries_dictionary)响应:
[
{'CountryId': 1, 'CountryName': 'Belgium', 'Abbreviation': 'B'},
...............................................................
...............................................................
{'CountryId': 26, 'CountryName': 'Great Britain', 'Abbreviation': 'GB'}
]根据以括号开头的结果,响应是一个列表。根据Python和JSON之间的等价物,列表被翻译成数组。但是,如果我想提取第一个键的值,就会得到一个错误:
码
for country in countries_dictionary['CountryId'][0]:
if country['CountryId']=='1':
print('Test')错误:
文件“c:/Python3.7/api.py”,第49行,国家中的国家_字典‘country’:TypeError:字符串索引必须是整数
我知道索引是用整数数据类型表示的,而且由于我得到的响应没有任何父节点,所以我有点迷路了,不知道下一步如何进行。到目前为止,我在Stackoverflow或任何其他网站上发现的任何东西都没有起作用。有小费吗?
编辑:
[
{'CountryId': 1, 'CountryName': 'Romania', 'Abbreviation': 'RO'},
{'CountryId': 2, 'CountryName': 'Bulgaria', 'Abbreviation': 'BG'},
{'CountryId': 3, 'CountryName': 'Slovenia', 'Abbreviation': 'SI'},
{'CountryId': 4, 'CountryName': 'Olanda', 'Abbreviation': 'NL'},
{'CountryId': 5, 'CountryName': 'Polonia', 'Abbreviation': 'PL'},
{'CountryId': 6, 'CountryName': 'Belgia', 'Abbreviation': 'BE'},
{'CountryId': 7, 'CountryName': 'Germania', 'Abbreviation': 'DE'},
{'CountryId': 8, 'CountryName': 'Spania', 'Abbreviation': 'ES'},
{'CountryId': 9, 'CountryName': 'Portugalia', 'Abbreviation': 'PT'},
{'CountryId': 10, 'CountryName': 'Irlanda', 'Abbreviation': 'IE'},
{'CountryId': 11, 'CountryName': 'Austria', 'Abbreviation': 'AT'},
{'CountryId': 12, 'CountryName': 'Croatia', 'Abbreviation': 'HR'},
{'CountryId': 13, 'CountryName': 'Ungaria', 'Abbreviation': 'HU'},
{'CountryId': 14, 'CountryName': 'Suedia', 'Abbreviation': 'SE'},
{'CountryId': 15, 'CountryName': 'Slovacia', 'Abbreviation': 'SK'},
{'CountryId': 16, 'CountryName': 'Cehia', 'Abbreviation': 'CZ'},
{'CountryId': 17, 'CountryName': 'Danemarca', 'Abbreviation': 'DK'},
{'CountryId': 18, 'CountryName': 'Estonia', 'Abbreviation': 'EE'},
{'CountryId': 19, 'CountryName': 'Finlanda', 'Abbreviation': 'FI'},
{'CountryId': 20, 'CountryName': 'Franta', 'Abbreviation': 'FR'},
{'CountryId': 21, 'CountryName': 'Letonia', 'Abbreviation': 'LV'},
{'CountryId': 22, 'CountryName': 'Lituania', 'Abbreviation': 'LT'},
{'CountryId': 23, 'CountryName': 'Luxemburg', 'Abbreviation': 'LU'},
{'CountryId': 24, 'CountryName': 'Norvegia', 'Abbreviation': 'NO'},
{'CountryId': 25, 'CountryName': 'Marea Britanie', 'Abbreviation': 'GB'},
{'CountryId': 26, 'CountryName': 'Ucraina', 'Abbreviation': 'UA'}
]发布于 2019-10-16 17:45:53
根据您的字典示例列表,您的代码中出现了一些错误。
中指定“CountryId”
但这应该是的诀窍
countries_dictionary = [{'CountryId': 1, 'CountryName': 'Belgium', 'Abbreviation': 'B'},
{'CountryId': 26, 'CountryName': 'Great Britain', 'Abbreviation': 'GB'}]
for country in countries_dictionary:
if country['CountryId']==1:
print('Test')https://stackoverflow.com/questions/58418867
复制相似问题