因此,我正在获取一个包含约5,000个地址的电子表格,并将其从Google Maps Geolocation API发送出去,以获取经/纬度以及格式化后的地址值(此处为许多速记)。
我遇到的问题是他们没有使用命名的keys
来让我利用和根据可用的位置来回切换。例如,这是一个完全正常的地址;
{
"results" : [
{
"address_components" : [
{
"long_name" : "27502",
"short_name" : "27502",
"types" : [ "street_number" ]
},
{
"long_name" : "Antonio Parkway",
"short_name" : "Antonio Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Ladera Ranch",
"short_name" : "Ladera Ranch",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Orange County",
"short_name" : "Orange 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" : "92694",
"short_name" : "92694",
"types" : [ "postal_code" ]
}
]
例如,现在,这是子前提中的位置:
{
"results" : [
{
"address_components" : [
{
"long_name" : "117",
"short_name" : "117",
"types" : [ "subpremise" ]
},
{
"long_name" : "3401",
"short_name" : "3401",
"types" : [ "street_number" ]
},
{
"long_name" : "North Miami Avenue",
"short_name" : "N Miami Ave",
"types" : [ "route" ]
},
{
"long_name" : "Wynwood",
"short_name" : "Wynwood",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Miami",
"short_name" : "Miami",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Miami-Dade County",
"short_name" : "Miami-Dade County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Florida",
"short_name" : "FL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "33127",
"short_name" : "33127",
"types" : [ "postal_code" ]
}
]
此问题是在第一种情况下,address_components[0].short_name
将是我想要的street_number
,但在示例2中,address_components[0].short_name
实际上是批号。
以脚本方式,这使得无法准确地构建我的JSON,因为在某些区域,由于存在辅助地址列表,我的Zip Code
的值将为街道名称。
您如何建议我对此进行反驳,以确保我始终能准确获取所请求的数据,而不是当时那个位置的情况如何?
下面是一个如何弄乱事情的示例:
"731": {
"City": "Noblesville",
"Street": "Norell Ln",
"Type": "dsg",
"StreetNum": "13157",
"Zip": "US",
"State": "Hamilton County",
"Name": "Noblesville ",
"Region": "Ohio Valley",
"Long": "-85.93149269999999",
"StateAbr": "Hamilton County",
"Phone": "3177761687",
"Lat": "39.9901088"
}
情况并非总是如此,也许200-300个是错误的,而其余的都是正确的,但是必须猜测和希望非常低。
为了包括一些我的脚本:
$builtAddress = "$street $city $state $zip"
Write-Verbose "Built Address: $builtAddress"
$addressArray.Add($builtAddress) > $null
$req = Invoke-WebRequest "https://maps.googleapis.com/maps/api/geocode/json?address=$builtAddress&key=AIzaSyDRo-UGY91_EiB2DeYzBU21-3FcaqIanPo"
$location = $req.Content | ConvertFrom-Json
$lat = $location.results[0].geometry.location.lat
$long = $location.results[0].geometry.location.lng
$z = $location.results[0].address_components
$name = $z.
$phone = $phone
$region = $region
$objectProps = @{
$store = @{
Name = "$name"
Lat = "$lat"
Long = "$long"
StreetNum = "$streetNum"
Street = "$street"
City = "$city"
State = "$state"
StateAbr = "$stateAb"
Zip = "$zip"
Phone = "$phone"
Region = "$region"
Type = "$type"
}
}
Write-Verbose $store
$jsonObj = New-Object psobject -Property $objectProps | ConvertTo-Json -depth 100 | Out-File C:\Users\admin-dksc104694\Desktop\Map_Data\JSON\$outputFile -Append
Write-Output ',' | Out-File C:\Users\admin-dksc104694\Desktop\$outputFile -Append
}
答案 0 :(得分:1)
您可以根据以下类型进行过滤:
$StreetNumber = $z.Where({$_.types -contains 'street_number'})
,然后在$store
哈希表中,您可以使用点表示法来提取值
$store = @{
...
StreetNum = $StreetNumber.short_name
...
}