2017-08-25 2 views
-1

ich erfolgreich eine json.load (Antwort) Anfrage und in der Lage zu navigieren/sehen das Ergebnis und erscheinen Füllung zu sein, was ich erwarte. Ich erhalte jedoch einen KeyError, wenn ich versuche, auf Attribute zuzugreifen. In diesem Fall muss ich eine lokale Variable auf das Attribut "SHORT_NAME" setzen.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 Jupiter Inlet ' u'OBJECTID': 17}}]}

Mein python-Code, um das oben:

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'" 

Es ist offensichtlich, dass "SHORT_NAME" da ist, also bin ich mir nicht ganz sicher, was ich falsch mache.

Vielen Dank für eine Rückmeldung!

+0

Wie stimmen Sie eine Frage nach unten? geesh. – user2309282

+1

In der for-Zeile benennen Sie die Variable featureAp, verwenden dann jedoch eine Feature-Variable in der Schleife. Ich nehme an, das ist ein Tippfehler? – lancew

+0

BINGO! Danke, dass du das eingefangen hast. Einfach! – user2309282

Antwort

1

Wechsel:

aqp = feature['attributes']['SHORT_NAME'] 

An:

aqp = featureAp['attributes']['SHORT_NAME'] 
+0

Dort gehen wir. Sie haben es im obigen Kommentar richtig beantwortet. Kann dies als Post-Antwort markieren. Danke für dein scharfes Auge! – user2309282