3
我想解析來自GoogleMaps API的JSON響應,將嵌套字典中的值讀入Pandas DataFrame。下面顯示了JSON響應的結構。GoogleMaps API Json使用ijson解析
我試圖創建一個名爲'address_components'與('類型','short_name','long_name')的數據框作爲列標題和每個地址查詢各自的值作爲行。
我試着使用ijson:
from ijson import items
import ijson
f = urlopen('https://maps.googleapis.com/maps/api/geocode/json?address=2022+Boren+Ave%2C+Seattle+98101')
objects = list(ijson.items(f, 'results.address_components'))
objects
日期:[]
我的問題是成功穿越嵌套類型的字典。
我也回顧了GoogleMaps GeoLocator API Documentation和GoogleMaps JSON parsing docs。 This solution seems to solve the problem, but on IOS
不幫我將手伸入巢...
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
太棒了!非常感謝。這正是我所期待的。很明顯,我還沒有被允許這麼正式地說出來,因爲當我檢查這個盒子時,它告訴我我不是我的問題的可信的答覆者......無論如何,非常感謝。 –