gpt4 book ai didi

iphone - 在Google JSON之外获取邮政编码

转载 作者:行者123 更新时间:2023-12-01 19:13:06 26 4
gpt4 key购买 nike

我正在尝试从经度和纬度检索邮政编码。因此,我使用google API。

这是我的要求。

http://maps.googleapis.com/maps/api/geocode/json?latlng=51.04166373133121,5.580196380615234&sensor=true

当您在浏览器中输入此名称时,您将看到整个JSON。这只是其中的一部分。

JSON文件

   {

"results": [
{
"address_components": [
{
"long_name": "2",
"short_name": "2",
"types": [
"street_number"
]
},
{
"long_name": "Hermisweg",
"short_name": "Hermisweg",
"types": [
"route"
]
},
{
"long_name": "Opglabbeek",
"short_name": "Opglabbeek",
"types": [
"locality",
"political"
]
},
{
"long_name": "Limburg",
"short_name": "LI",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Vlaams Gewest",
"short_name": "Vlaams Gewest",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "België",
"short_name": "BE",
"types": [
"country",
"political"
]
},
{
"long_name": "3660",
"short_name": "3660",
"types": [
"postal_code"
]
}
]
}
]
}

我的问题是现在如何从JSON中获取邮政编码?在此示例中,我希望将3660作为输出。

有人知道吗?

最佳答案

connectionDidFinishLoading中尝试一下。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *thexml=[[[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding] autorelease];

NSError *jsonError = nil;
id allValues = [NSJSONSerialization JSONObjectWithData:[thexml dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&jsonError];

if(jsonError!=nil)
NSLog(@"JsonError: %@",jsonError);


NSArray *result = [(NSDictionary*)allValues objectForKey:@"results"];
for(int i=0;i<[result count];i++)
{
NSDictionary *values = (NSDictionary*)[result objectAtIndex:i];

NSArray *component = [(NSDictionary*)values objectForKey:@"address_components"];

for(int j=0;j<[component count];j++)
{
NSDictionary *parts = (NSDictionary*)[component objectAtIndex:j];
if([[parts objectForKey:@"types"] containsObject:@"postal_code"])
{
NSLog(@"Postal Code : %@ ",[parts objectForKey:@"long_name"]);
break;
}
}
}
}

关于iphone - 在Google JSON之外获取邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15109564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com