我成功地填充了一個json.load(響應)請求並能夠導航/查看結果,並且看起來是我期待的。然而,當我嘗試訪問屬性時,我得到一個KeyError。在這種情況下,我需要將一個局部變量設置爲「SHORT_NAME」屬性。python 2.7 urllib2.Request KeyError
{u'fieldAliases ':{u'SHORT_NAME':u'SHORT_NAME」,u'OBJECTID ': u'OBJECTID'},u'fields ':[{u'alias':u'OBJECTID ',u'type': u'esriFieldTypeOID',u'name':u'OBJECTID'},{u'alias':u'SHORT_NAME', u'length':50,u'type':u' esriFieldTypeString',u'name': u'SHORT_NAME'}],u'displayFieldName':u'LONG_NAME',u'features': [{u'attributes':{u'SHORT_NAME':u'Jensen Beach to木星進 ' u'OBJECTID':17}}]}
我的Python代碼訪問上述:
reqAp = urllib2.Request(queryURLAp, paramsAp)
responseAp = urllib2.urlopen(reqAp)
jsonResultAp = json.load(responseAp) #all good here! above example is what this contains
#trying to set variable to the SHORT_NAME attribute
for featureAp in jsonResultAp['features']:
aqp = feature['attributes']['SHORT_NAME']
#this fails with: "KeyError: 'SHORT_NAME'"
很明顯,「SHORT_NAME」在那裏,所以我不太確定我做錯了什麼。
感謝您的任何反饋!
你怎麼否決一個問題嗎? geesh。 – user2309282
在for行中,您將變量featureAp命名,但在循環中使用了一個特性變量。我認爲這是一個錯字? – lancew
賓果!謝謝你收到。簡單! – user2309282