Python解析JSON

2024-10-02 22:29:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试编写一个程序来提示输入位置,联系web服务并检索web服务的JSON并解析数据,并从JSON中检索第一个位置的_id。在

import urllib
import json
import ssl
serviceurl = 'https://pr4e.dr-chuck.com/tsugi/mod/python-data/data/geojson?'

address = raw_input('Enter location: ')
url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
print 'Retrieving', url
scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
uh = urllib.urlopen(url, context=scontext)
data = uh.read()
print 'Retrieved',len(data),'characters'

try: 
    js = json.loads(str(data))
except: 
    js = None

if 'status' not in js or js['status'] != 'OK':
    print '==== Failure To Retrieve ===='
    print data


placeID = js['results'][0]['place_id']
print 'Place id: ', placeID

当我运行最后2行代码时,我偶然发现了这个错误。在

^{pr2}$

你能帮我修一下吗!?为什么会这样?在


Tags: importwebidjsonurlssldataaddress
1条回答
网友
1楼 · 发布于 2024-10-02 22:29:58

然后你进入了一个不存在的地方,我想。当我运行代码时,它运行得非常好。以下是我对有效位置Wayne State的输出:

Enter location: Wayne State
Retrieving https://pr4e.dr-chuck.com/tsugi/mod/python-data/data/geojson?sensor=false&address=Wayne+State
Retrieved 2280 characters

然后js['results'][0]['place_id']给了我:

^{pr2}$

整个js如下所示:

{u'results': [{u'address_components': [{u'long_name': u'State Avenue',
     u'short_name': u'State Ave',
     u'types': [u'route']},
    {u'long_name': u'Wayne',
     u'short_name': u'Wayne',
     u'types': [u'locality', u'political']},
    {u'long_name': u'Union',
     u'short_name': u'Union',
     u'types': [u'administrative_area_level_3', u'political']},
    {u'long_name': u'Wayne County',
     u'short_name': u'Wayne County',
     u'types': [u'administrative_area_level_2', u'political']},
    {u'long_name': u'West Virginia',
     u'short_name': u'WV',
     u'types': [u'administrative_area_level_1', u'political']},
    {u'long_name': u'United States',
     u'short_name': u'US',
     u'types': [u'country', u'political']},
    {u'long_name': u'25570',
     u'short_name': u'25570',
     u'types': [u'postal_code']}],
   u'formatted_address': u'State Ave, Wayne, WV 25570, USA',
   u'geometry': {u'bounds': {u'northeast': {u'lat': 38.2407197,
      u'lng': -82.43612209999999},
     u'southwest': {u'lat': 38.2390583, u'lng': -82.4396787}},
    u'location': {u'lat': 38.2407197, u'lng': -82.43794009999999},
    u'location_type': u'GEOMETRIC_CENTER',
    u'viewport': {u'northeast': {u'lat': 38.24123798029149,
      u'lng': -82.43612209999999},
     u'southwest': {u'lat': 38.23854001970849, u'lng': -82.4396787}}},
   u'partial_match': True,
   u'place_id': u'ChIJ4Uny3H3vRYgRXPqO3llpGmg',
   u'types': [u'route']}],
 u'status': u'OK'}

当您在一个无效的位置运行它时(例如stuff),那么您在js中没有键results,这会导致您得到错误。这里是这个场景的输出(仅仅是第一行)(然后只有键error和{}导致您观察到的key error):

Enter location: stuff
Retrieving https://pr4e.dr-chuck.com/tsugi/mod/python-data/data/geojson?sensor=false&address=stuff
Retrieved 10041 characters
==== Failure To Retrieve ====
{
  "error":"Address not found in the list of available locations",
  "locations":[
    "AGH University of Science and Technology",
    "Academy of Fine Arts Warsaw Poland",
    "American University in Cairo",
    "Arizona State University",
    "Athens Information Technology",

所以,一定要选择一个在有效位置列表中的位置。在

相关问题 更多 >