在Python中解析Google Geocode JSON

时间:2016-02-03 15:00:32

标签: python json parsing google-geocoder

我是python的新手,并尝试使用Python 2.7解析Google地理编码响应中的JSON。可以在此处找到整个JSON文件:https://dl.dropboxusercontent.com/u/60455118/data.json

示例JSON:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "277",
               "short_name" : "277",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Bedford Avenue",
               "short_name" : "Bedford Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Williamsburg",
               "short_name" : "Williamsburg",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "Kings County",
               "short_name" : "Kings County",
               "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" ]
            },
            {
               "long_name" : "11211",
               "short_name" : "11211",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "277 Bedford Ave, Brooklyn, NY 11211, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.714232,
               "lng" : -73.9612889
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.7155809802915,
                  "lng" : -73.9599399197085
               },
               "southwest" : {
                  "lat" : 40.7128830197085,
                  "lng" : -73.96263788029151
               }
            }
         },

我希望输出格式为:

227, Bedford Avenue, Williamsburg, Brooklyn, Kings County, New York, United States, 11211, Latitude, Longitude

我的脚本如下:

import json
from pprint import pprint

with open('c:\scripts\data.json') as f:
   data = json.load(f)


coordinates = data["results"][0]["geometry"]["location"]


for i in data["results"][0]["address_components"]:
   print i["long_name"]

以这种格式输出:

277
Bedford Avenu
Williamsburg
Brooklyn
Kings County
New York
United States
11211

如何转置此项并添加Lat Lon值?

2 个答案:

答案 0 :(得分:2)

将值放入列表中,然后使用join

","
elements = []

coordinates = data["results"][0]["geometry"]["location"]

for i in data["results"][0]["address_components"]:
   elements.append( i["long_name"] )

elements.append( str(coordinates['lat']) ) # convert to string to use `join`
elements.append( str(coordinates['lng']) ) # convert to string to use `join`

print ", ".join(elements)

-

277, Bedford Avenue, Williamsburg, Brooklyn, Kings County, New York, United States, 11211, 40.714232, -73.9612889

答案 1 :(得分:-1)

我不明白你的问题是什么:

for result in data["results"]:
    address = address = {place['types'][0]: place ['long_name'] for place in data["address_components"]}
    address['lat'] = float(result["geometry"]["location"]['lat'])
    address['lng'] = float(result["geometry"]["location"]['lng'])
    print address