Python 提取JSON文件的信息
我通过Python 提取JSON文件的信息,python,json,Python,Json,我通过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'
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]}]}
通过结果['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]}]
我想在navn
我尝试了所有的组合
results['features']['properties']['navn']
results['features']['navn']
results['features']['properties']
它们都显示相同的错误消息:列表索引必须是整数或片,而不是str
显然,results['features']
是一个长度为1的列表
如何获取navn
信息
你可以想象,我想打几个电话 results['features']对象是一个列表,请尝试
results['features'][0]['properties']['navn']
现在选择列表(0)中的第一个元素,即字典,然后从该字典中选择“navn”键。结果是“navn”的值
请注意,python列表在[]之间,项目用逗号分隔,python字典在{}之间,由逗号分隔的键和值对组成。结果['features']对象是一个列表,请尝试
结果['features'][0]['properties']['navn']
现在选择列表(0)中的第一个元素,即字典,然后从该字典中选择“navn”键。结果是“navn”的值
请注意,python列表在[]之间,项目用逗号分隔,python字典在{}之间,由逗号分隔的键、值对组成。您应该尝试访问
结果['features']
中列表的第一个元素,即:
results['features'][0]['properties']['navn']
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
您应该尝试访问结果['features']
中列表的第一个元素,即:
results['features'][0]['properties']['navn']
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
试试这个
results['features'][0]['properties']['navn']
试试这个
results['features'][0]['properties']['navn']
您可以尝试以下代码:
您可以尝试以下代码:
您出现错误,因为在功能中有一个列表。因此,您无法借助str index获取列表,而要获取功能中的属性,您需要编写[0]
,列表将消失,您可以获取值
您出现错误,因为在功能中有一个列表。因此,您无法借助str索引获取列表,也无法获取您需要编写的功能中的属性。
[0]
列表将消失,您可以获取值。这是因为结果['features']
是一个具有一个对象的列表类型,这是因为结果['features']
是一个具有一个对象的列表类型