我试图遍历json并仅获取对象的特征数组并获取所有对象,但是我似乎无法理解我在python代码中做错了什么。这是我的代码:
我已经查看了这些问题以寻求建议,但似乎没有用。 TypeError: string indices must be integers error when parsing Json with 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
答案 0 :(得分:1)
featureSet
不是列表,它是一个以geometryType
和features
为键的字典。
这样做的时候
for featureData in fs:
您正在遍历这些键。
如果您的意思是要遍历features
下的列表,则应该执行类似的操作
featureset['features'][i]
或
for feature in featureSet['features']