【发布时间】:2020-07-13 19:36:47
【问题描述】:
我有一些看起来像这样的 json 数据:
'{"type": "FeatureCollection",
"features": [{"id": "0",
"type": "Feature",
"properties": {},
"geometry": {"type": "Polygon",
"coordinates": [
[[-2.9611591576420615, 53.43052239401445],
[-2.9602608423579424, 53.43052239401445],
[-2.9602608423579424, 53.4310576043007],
[-2.9611591576420615, 53.4310576043007],
[-2.9611591576420615, 53.43052239401445]]
]}
}]
}'
我希望我的代码能够按原样仅检索“坐标”,因此我尝试使用此代码:
points = json_data['features']['geometry']['coordinates']
print(points)
但是当我运行它时,我得到了这个错误:TypeError: string indices must be integers
我已经尝试了很多方法来尝试解决这个问题,但没有成功,所以我想知道是否有人知道我该如何解决这个问题?
提前致谢!
【问题讨论】:
-
那不是json,是字符串。您需要先将其解析为对象。
-
json_data['features'][0]['geometry']['coordinates'] -
“解析成对象”是什么意思?
-
另外,@OlvinRoght 添加 [0] 仍然给我一个打字错误
-
x = json.loads(json_data)
标签: python json dataframe typeerror