TypeError: string must be indices on JSON looping through feature array of objects in arcpy
TypeError: string must be indices on JSON looping through feature array of objects in arcpy
我试图遍历我的 json 并且只获取对象的特征数组并获取所有对象,但我似乎无法理解我的 python 代码哪里做错了.这是我的代码:
我查看了这些问题以寻求建议,但似乎没有用。
jsonData = operationalLayers = None
#for testing purposes, parse from a json file
if Web_Map_as_JSON != '':
jsonData = json.loads(Web_Map_as_JSON)
operationalLayers = jsonData["operationalLayers"]
arcpy.AddMessage("We have valid json data")
else:
with open('json/WLA-FRI-AY-14-subset.json') as data:
jsonData = json.load(data)
operationalLayers = jsonData["operationalLayers"]
print "We parsed json data from a file"
# looping thru only graphics-operation-layer and its sub layers
# and add attributes for each features on the layout
for ol in operationalLayers:
#hard code the first feature layer
if (ol["id"] == "ParcelRouteEditingTest_1259"):
if (ol.has_key('featureCollection')):
fcol = ol["featureCollection"]
if (not fcol.has_key('layers')):
continue
lyrs = fcol["layers"]
#loop through layer
for i in lyrs:
# feature set data on the graphic layer
fs = i["featureSet"]
# store attributes for easy access
for featureData in fs:
featureDataObj = featureData[1]["features"]
print featureDataObj
# queryURL = '{0}/query'.format(i['url'])
# arcpy.AddMessage(queryURL)
# getQuery = requests.get(queryURL)
# if(getQuery.status_code == 200):
# fs = getQuery.json()
updateLayoutElementWithFeatureAttributes(feature_data_obj, None, pdfPaths)
这是 json 数据的结构:
错误:
Traceback (most recent call last):
File "C:\Workspace-TT\pythonprint\customPrint.py", line 299, in <module>
featureDataObj = featureData[1]["features"]
TypeError: string indices must be integers
featureSet
不是一个列表,它是一个以 geometryType
和 features
为键的字典。
当你这样做时
for featureData in fs:
您正在遍历这些键。
如果您的意思是遍历 features
下的列表,您应该执行类似
的操作
featureset['features'][i]
或
for feature in featureSet['features']
我试图遍历我的 json 并且只获取对象的特征数组并获取所有对象,但我似乎无法理解我的 python 代码哪里做错了.这是我的代码:
我查看了这些问题以寻求建议,但似乎没有用。
jsonData = operationalLayers = None
#for testing purposes, parse from a json file
if Web_Map_as_JSON != '':
jsonData = json.loads(Web_Map_as_JSON)
operationalLayers = jsonData["operationalLayers"]
arcpy.AddMessage("We have valid json data")
else:
with open('json/WLA-FRI-AY-14-subset.json') as data:
jsonData = json.load(data)
operationalLayers = jsonData["operationalLayers"]
print "We parsed json data from a file"
# looping thru only graphics-operation-layer and its sub layers
# and add attributes for each features on the layout
for ol in operationalLayers:
#hard code the first feature layer
if (ol["id"] == "ParcelRouteEditingTest_1259"):
if (ol.has_key('featureCollection')):
fcol = ol["featureCollection"]
if (not fcol.has_key('layers')):
continue
lyrs = fcol["layers"]
#loop through layer
for i in lyrs:
# feature set data on the graphic layer
fs = i["featureSet"]
# store attributes for easy access
for featureData in fs:
featureDataObj = featureData[1]["features"]
print featureDataObj
# queryURL = '{0}/query'.format(i['url'])
# arcpy.AddMessage(queryURL)
# getQuery = requests.get(queryURL)
# if(getQuery.status_code == 200):
# fs = getQuery.json()
updateLayoutElementWithFeatureAttributes(feature_data_obj, None, pdfPaths)
这是 json 数据的结构:
错误:
Traceback (most recent call last):
File "C:\Workspace-TT\pythonprint\customPrint.py", line 299, in <module>
featureDataObj = featureData[1]["features"]
TypeError: string indices must be integers
featureSet
不是一个列表,它是一个以 geometryType
和 features
为键的字典。
当你这样做时
for featureData in fs:
您正在遍历这些键。
如果您的意思是遍历 features
下的列表,您应该执行类似
featureset['features'][i]
或
for feature in featureSet['features']