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

标签 iphone ios objective-c json google-maps-api-3

我正在尝试从经度和纬度检索邮政编码。因此,我使用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/

相关文章:

iphone - 在 Objective C 中构建加密 key 存储库?

iphone - 如何在不进行任何格式化的情况下从 NSDecimal 获取字符串表示形式?

objective-c - 在 iPhone 上实现自定义 UITextView 界面的技巧?

ios - UIDatePicker 减去确切时间 - Swift

ios - 为什么我不能在 iOS 中的任何 map 应用程序上直接进行深度链接以逐个导航?

ios - 可以填充选择器 View 但不能从中获取数据

iphone - 如何使用 JSON-Framework 解析 iPhone SDK (XCode) 中的 JSON 对象

ios - 从今天扩展开始 NSURLSessionDownloadTask

ios - theos - 未知类型名称

ios - 如何使用 Parse 保存 UITextField?