0

我正在尝试解析我收到的 json 数据

    {'results': [{'address_components': [{'long_name': 'New York',
                                      'short_name': 'New York',
                                      'types': ['locality', 'political']},
                                     {'long_name': 'New York',
                                      'short_name': 'NY',
                                      'types': ['administrative_area_level_1',
                                                'political']},
                                     {'long_name': 'United States',
                                      'short_name': 'US',
                                      'types': ['country', 'political']}],
              'formatted_address': 'New York, NY, USA',
              'geometry': {'bounds': {'northeast': {'lat': 40.9152414,
                                                    'lng': -73.70027209999999},
                                      'southwest': {'lat': 40.495908,
                                                    'lng': -74.2590879}},
                           'location': {'lat': 40.7143528,
                                        'lng': -74.00597309999999},
                           'location_type': 'APPROXIMATE',
                           'viewport': {'northeast': {'lat': 40.9152414,
                                                      'lng': -73.70027209999999},
                                        'southwest': {'lat': 40.4959143,
                                                      'lng': -74.2557349}}},
              'types': ['locality', 'political']},
             {'address_components': [{'long_name': 'Manhattan',
                                      'short_name': 'Manhattan',
                                      'types': ['sublocality',
                                                'political']},
                                     {'long_name': 'New York',
                                      'short_name': 'New York',
                                      'types': ['locality', 'political']},
                                     {'long_name': 'New York',
                                      'short_name': 'New York',
                                      'types': ['administrative_area_level_2',
                                                'political']},
                                     {'long_name': 'New York',
                                      'short_name': 'NY',
                                      'types': ['administrative_area_level_1',
                                                'political']},
                                     {'long_name': 'United States',
                                      'short_name': 'US',
                                      'types': ['country', 'political']}],
              'formatted_address': 'Manhattan, New York, NY, USA',
              'geometry': {'bounds': {'northeast': {'lat': 40.882214,
                                                    'lng': -73.907},
                                      'southwest': {'lat': 40.6795479,
                                                    'lng': -74.047285}},
                           'location': {'lat': 40.7902778,
                                        'lng': -73.9597222},
                           'location_type': 'APPROXIMATE',
                           'viewport': {'northeast': {'lat': 40.820045,
                                                      'lng': -73.90331300000001},
                                        'southwest': {'lat': 40.698078,
                                                      'lng': -74.03514899999999}}},
              'partial_match': True,
              'types': ['sublocality', 'political']}],
 'status': 'OK'}
[
   {
      "lat": 40.7143528, 
      "lng": -74.00597309999999
   }, 
   {
      "lat": 40.7902778, 
      "lng": -73.9597222
   }
]

例如,我如何能够解析“结果”?我想打印出结果中的所有数据。我的代码是...

import urllib, json
import pprint

URL2 = "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false"

googleResponse = urllib.urlopen(URL2)
jsonResponse = json.loads(googleResponse.read())
pprint.pprint(jsonResponse)
test = json.dumps([s['geometry']['location'] for s in jsonResponse['results']], indent=3)
print test['results']

我得到的错误是

回溯(最近一次通话最后):

文件“C:/Python27/jstox.py”,第 10 行,在

打印测试['结果']

TypeError:字符串索引必须是整数,而不是 str

4

1 回答 1

0

你实际上已经得到了你想要的结果。

import urllib, json
import pprint

URL2 = "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false"

googleResponse = urllib.urlopen(URL2)
jsonResponse = json.loads(googleResponse.read())
pprint.pprint(jsonResponse)
pprint.pprint(jsonResponse['results'])

如果您熟悉这些,在这种情况下,可以将 json 数据与 Python字典类似地处理。

于 2013-02-17T04:29:52.207 回答