[英]Getting the Postal code outside the google JSON
I'm trying to retrieve a postal code from a longitude and latitude. 我正试图从经度和纬度中检索邮政编码。 Therefore I use the google API. 因此我使用谷歌API。
Here is my request. 这是我的要求。
http://maps.googleapis.com/maps/api/geocode/json?latlng=51.04166373133121,5.580196380615234&sensor=true http://maps.googleapis.com/maps/api/geocode/json?latlng=51.04166373133121,5.580196380615234&sensor=true
When you enter this in you browser you will see the entire JSON. 当您在浏览器中输入此内容时,您将看到整个JSON。 Here is only a part of it. 这只是其中的一部分。
JSON File 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"
]
}
]
}
]
}
My question is now how I can get the postal code from this JSON? 我现在的问题是如何从这个JSON获取邮政编码? In this example I want 3660 as output. 在这个例子中,我想要3660作为输出。
Anybody got an idea? 有人有个主意吗?
Try this inside connectionDidFinishLoading
. 在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;
}
}
}
}
You want "3600" whose key is "long_name" ... so you want "long_name" basically . 你想要“3600”,其键是“long_name”...所以你基本上想要“long_name”。
Try This :- 尝试这个 :-
suppose you are getting all the result in resultDict . 假设您在resultDict中获得了所有结果。
NSString *long_name = [[[[[resultDict objectForKey:@"results"] objectAtIndex:0] objectForKey:@"address_components"] objectAtIndex:0] objectForKey:@"long_name"];
NSLog(@"long_name is %@",long_name);
Note :- You will have to replace the 0 with the for loop's variable (for example "i") , put there the number of elements in array you get in the result dictionary. 注意: - 你必须用for循环的变量替换0(例如“i”),把你在结果字典中得到的数组中的元素数放在那里。
Map this JSON into a NSDictionary class. 将此JSON映射到NSDictionary类。 From this you can access the address_components array. 从这里你可以访问address_components数组。
Use array objectAtIndex to get required value. 使用array objectAtIndex获取所需的值。
IF you need a comprehensive solution go for Abstract KVC wrapper classes available in github 如果您需要全面的解决方案,请参阅github中提供的Abstract KVC包装类
These directly map JSON into model classes. 这些直接将JSON映射到模型类。
Once mapped, you can access these by specifying : object.property of these classes. 映射后,您可以通过指定这些类的:object.property来访问它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.