提取JSON文件的信息

问题描述 投票:0回答:4

我通过results = requests.get(url).json()获得了结果结果看起来像这样:

{'type':'FeatureCollection','crs':{'type':'name','properties':{'name':'EPSG:4326'}},'features':[{'type':'Feature','properties':{'kode':'0101','navn':'København','region_kode':'1084.0','region_navn':'Hovedstaden'},'bbox':[12.453042062098154,55.612994971371606,12.734252598475942,55.732491190632494]}]}

具有结果['功能'],我得到了这个

[{'type':'Feature','properties':{'kode':'0101','navn':'København','region_kode':'1084.0','region_navn':'Hovedstaden'},'bbox':[12.453042062098154,55.612994971371606,12.734252598475942,55.732491190632494]}]

我想在“ navn”中获取信息我尝试了

的所有组合
results['features']['properties']['navn']
results['features']['navn']
results['features']['properties']

它们都显示相同的错误消息:列表索引必须是整数或切片,而不是str

显然,results ['features']是一个长度为1的列表。

我如何获得“导航”信息?

我想打个电话,你可以想像。

python json
4个回答
0
投票

results['features']对象是一个列表,请尝试results['features'][0]['properties']['navn']

现在选择列表(0)中的第一个元素,即字典,然后从该字典中选择'navn'键。结果是'navn'

的值

请注意,python列表在[]之间,并且项目之间用逗号分隔,而python字典在{}之间,并且由键,值对组成,并用逗号分隔。


0
投票

尝试一下

results['features'][0]['properties']['navn']

0
投票

您可以在下面尝试代码:

results['features'][0]['properties']['navn']

0
投票

您应该尝试访问listresult['features']的第一个元素,即:

results['features'][0]['properties']['navn']

完整代码:

results = {'type': 'FeatureCollection', 'crs': {'type': 'name', 'properties': {'name': 'EPSG:4326'}}, 'features': [{'type': 'Feature',
'properties': {'kode': '0101', 'navn': 'København', 'region_kode': '1084.0', 'region_navn': 'Hovedstaden'}, 'bbox': [12.453042062098154, 55.612994971371606, 12.734252598475942, 55.732491190632494]}]}
print(results['features'][0]['properties']['navn'])
# København
© www.soinside.com 2019 - 2024. All rights reserved.