[英]how to solve TypeError: list indices must be integers or slices, not str in python
The Situation情况
I am trying to access values within a json string but receive the error:我试图访问 json 字符串中的值但收到错误:
TypeError: list indices must be integers or slices, not str
类型错误:列表索引必须是整数或切片,而不是 str
The JSON JSON
j={
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
2.44775,
48.925778
]
},
"properties": {
"country_code": "fr",
"housenumber": "15",
"street": "Rue Leon Bernard",
"distance": 16.948223707815888,
"country": "France",
"county": "Seine-Saint-Denis",
"datasource": {
"sourcename": "openaddresses",
"attribution": "© OpenAddresses contributors",
"license": "BSD-3-Clause License"
},
"postcode": "93700",
"state": "Ile-of-France",
"district": "Drancy",
"city": "Drancy",
"lon": 2.44775,
"lat": 48.925778,
"result_type": "building",
"formatted": "15 Rue Leon Bernard, 93700 Drancy, France",
"address_line1": "15 Rue Leon Bernard",
"address_line2": "93700 Drancy, France",
}
}
]
}
The Objective目标
I need to get the value of country_code and country i have tried我需要获取country_code 和country 的值我已经尝试过
j['features']['properties'][0]`
The Question:问题:
How can i get the value of these two fields in python ?如何在python中获取这两个字段的值?
In the json that you provided the 'features' value is a list, thus you need to index it accordingly.在您提供的 json 中,'features' 值是一个列表,因此您需要相应地对其进行索引。
j['features'][0]['properties']
From here you should be able to simply continue to access all the values of 'properties'从这里您应该能够简单地继续访问“属性”的所有值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.