【问题标题】:Parsing JSON file with Python -> google map api使用 Python 解析 JSON 文件 -> google map api
【发布时间】:2011-06-06 01:48:14
【问题描述】:

我正在尝试在 Python 中开始使用 JSON,但似乎我误解了 JSON 概念中的某些内容。我跟着google api example,效果很好。但是当我在 JSON 响应中将代码更改为较低级别时(如下所示,我尝试访问该位置),我收到以下代码的以下错误消息:

回溯(最近一次通话最后一次):
文件“geoCode.py”,第 11 行,在
<module>
test = json.dumps([s['location'] for s in jsonResponse['results']], indent=3) KeyError: '位置'

如何在 python 中访问 JSON 文件中较低的信息级别? 是否必须转到更高级别并搜索结果字符串?这对我来说似乎很奇怪?

这是我尝试运行的代码:

import urllib, json
URL2 = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"
googleResponse = urllib.urlopen(URL2);
jsonResponse = json.loads(googleResponse.read())
test = json.dumps([s['location'] for s in jsonResponse['results']], indent=3)
print test

【问题讨论】:

    标签: python json api parsing google-maps


    【解决方案1】:

    首先混淆它,因为导入json无法加载,它必须是

    将 simplejson 导入为 json,所以:

    import urllib
    import simplejson as json
    import pprint
    
    URL2 = "http://pbx/a/kiosks"
    
    googleResponse = urllib.urlopen(URL2)
    jsonResponse = json.loads(googleResponse.read())
    pprint.pprint(jsonResponse)
    

    【讨论】:

      【解决方案2】:

      理解jsonResponse的格式的关键是打印出来:

      import urllib, json
      import pprint
      
      URL2 = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"
      
      googleResponse = urllib.urlopen(URL2)
      jsonResponse = json.loads(googleResponse.read())
      pprint.pprint(jsonResponse)
      # {u'results': [{u'address_components': [{u'long_name': u'1600',
      #                                         u'short_name': u'1600',
      #                                         u'types': [u'street_number']},
      #                                        {u'long_name': u'Amphitheatre Pkwy',
      #                                         u'short_name': u'Amphitheatre Pkwy',
      #                                         u'types': [u'route']},
      #                                        {u'long_name': u'Mountain View',
      #                                         u'short_name': u'Mountain View',
      #                                         u'types': [u'locality',
      #                                                    u'political']},
      #                                        {u'long_name': u'San Jose',
      #                                         u'short_name': u'San Jose',
      #                                         u'types': [u'administrative_area_level_3',
      #                                                    u'political']},
      #                                        {u'long_name': u'Santa Clara',
      #                                         u'short_name': u'Santa Clara',
      #                                         u'types': [u'administrative_area_level_2',
      #                                                    u'political']},
      #                                        {u'long_name': u'California',
      #                                         u'short_name': u'CA',
      #                                         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'94043',
      #                                         u'short_name': u'94043',
      #                                         u'types': [u'postal_code']}],
      #                u'formatted_address': u'1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA',
      #                u'geometry': {u'location': {u'lat': 37.4216227,
      #                                            u'lng': -122.0840263},
      #                              u'location_type': u'ROOFTOP',
      #                              u'viewport': {u'northeast': {u'lat': 37.424770299999999,
      #                                                           u'lng': -122.0808787},
      #                                            u'southwest': {u'lat': 37.418475100000002,
      #                                                           u'lng': -122.0871739}}},
      #                u'types': [u'street_address']}],
      #  u'status': u'OK'}
      
      test = json.dumps([s['geometry']['location'] for s in jsonResponse['results']], indent=3)
      print(test)
      # [
      #    {
      #       "lat": 37.4216227, 
      #       "lng": -122.0840263
      #    }
      # ]
      
      1. jsonResponse 是一个字典。
      2. jsonResponse['results'] 是一个字典列表。
      3. 循环for s in jsonResponse['results'] 分配 s 每次迭代的字典 通过循环。
      4. s['geometry'] 是一个字典。
      5. s['geometry']['location'] (终于!)包含 纬度/经度字典。

      【讨论】:

        猜你喜欢
        • 2020-10-07
        • 2016-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 2015-12-25
        • 1970-01-01
        相关资源
        最近更新 更多