我從那個看起來像下面的API JSON響應索引:JSON索引類型錯誤:字符串索引必須是整數
[{
"id": 1
"name": "Gimbal HQ",
"addressLineOne": "5775 Morehouse Drive, San Diego CA 92121",
"geoFenceCircle": {
"radius": 100,
"visibility": "ORGANIZATION",
"location": {
"latitude": 32.89494374592149,
"longitude": -117.19603832579497
}
},
"placeAttributes": {
"key1": "value1",
"key2": "value2"
}
},
{
"id": 3291
"name": "Gimbal R&D",
"addressLineOne": "5665 Morehouse Drive, San Diego CA 92121",
"geoFencePolygon": {
"visibility": "ORGANIZATION",
"locations": [
{
"latitude": 32.8953153522896,
"longitude": -117.19559844351653
},
{
"latitude": 32.8954009341414,
"longitude": -117.19516929007415
},
{
"latitude": 32.89564867061472,
"longitude": -117.1949815354431
},
{
"latitude": 32.89545949009762,
"longitude": -117.19463284827117
},
{
"latitude": 32.894986537037255,
"longitude": -117.19496544218902
},
{
"latitude": 32.894864920127866,
"longitude": -117.19554479933623
}
]
},
"placeAttributes": {
"key1": "value1",
"key2": "value2"
}
}]
這就是所有罰款和花花公子。但我的問題是我的代碼,特別是這一行:
for geofence in geofences:
obj, created = Geofence.objects.get_or_create(name=geofence['name'])
我似乎提高了以下錯誤:
21:17:17 web.1 | obj, created = Geofence.objects.get_or_create(name=geofence['name'])
21:17:17 web.1 | TypeError: string indices must be integers
我真的不能找出問題是什麼,任何人都可以擺脫對此有所瞭解?
似乎'geofences'不是你認爲的那樣。錯誤表明你已經擊中了一個字符串,而不是預期的字典。 – monkut
只需在終端中進行雙重檢查,geofences實際上是JSON的一個更改(不同信息,不是格式)版本。所以地理圍欄就是我的想法。除非我錯過了一些東西。 – ApathyBear
如果'geofences'是一個字典列表,那麼你不應該看到'TypeError:字符串索引必須是整數'。 – monkut