I'm trying to retrieve a postal code from a longitude and latitude. Therefore I use the google API.
Here is my request.
When you enter this in you browser you will see the entire JSON. Here is only a part of it.
JSON File
{
"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? In this example I want 3660 as output.
Anybody got an idea?
Try this inside 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 .
Try This :-
suppose you are getting all the result in 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.
Map this JSON into a NSDictionary class. From this you can access the address_components array.
Use array objectAtIndex to get required value.
IF you need a comprehensive solution go for Abstract KVC wrapper classes available in github
These directly map JSON into model classes.
Once mapped, you can access these by specifying : object.property of these classes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.