使用JSonKit进行地理编码API

时间:2012-02-04 20:21:37

标签: objective-c google-maps mapkit geocoding jsonkit

我在解析Google地理编码API响应方面遇到了麻烦。这是方法:

-(void) findLatLong:(NSString*) adr{

NSString *encodedAddress = (NSString *) CFURLCreateStringByAddingPercentEscapes(
                                                                                NULL, (CFStringRef) adr,
                                                                                NULL, (CFStringRef) @"!*'();:@&=+$,/?%#[]",
                                                                                kCFStringEncodingUTF8 );

NSString* searchURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true",encodedAddress];
NSError* error = nil;
NSURLResponse* response = nil;
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] init] autorelease];

NSURL* URL = [NSURL URLWithString:searchURL];
[request setURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:30];

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (error){
    NSLog(@"Error performing request %@", searchURL);
    return;
}

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *results = [jsonString objectFromJSONString];
// to be added
}

我可以从Geococing API获得响应json字符串。当我用JSonKit解析它时,我有两个键。这些是结果状态。但是,如果我更进一步,如下所示:

[[results objectForKey:@"results"] objectForKey:@"address_components"]

我总是得到空集。有什么建议吗?

我正在使用XCode 4.2.1

0 个答案:

没有答案