2013-02-27 2 views
1

위도와 경도에서 우편 번호를 검색하려고합니다. 따라서 Google API를 사용합니다.Google JSON 외부에서 우편 번호 받기

내 요청 사항입니다.

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을 출력으로 원합니다.

아무도 아이디어가 없습니까?

+1

그럼 궁금한 점은 무엇입니까? JSON을 읽는 방법? 이는 Google지도와 관련이 없습니다. – Marcelo

+0

@Marcelo 나는 json을 읽는 법을 안다. 하지만 나는 json에서 우편 번호를 얻을 수 없습니다. – Steaphann

답변

2

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; 
      } 
     } 
    } 
} 
+0

이것은 효과가있었습니다! 고맙습니다 ! – Steaphann

0

이 JSON을 NSDictionary 클래스에 매핑하십시오. 여기에서 address_components 배열에 액세스 할 수 있습니다.

arrayActIndex를 사용하여 필요한 값을 가져옵니다.

당신이 포괄적 인 솔루션은 직접 모델 클래스에 JSON 매핑이

github에

에서 사용할 수있는 추상 KVC 래퍼 클래스에 갈 필요가 경우.

매핑되면 다음 클래스를 지정하여 액세스 할 수 있습니다.이 클래스의 object.property.

1

키가 "long_name"인 "3600"을 원하므로 기본적으로 "long_name"을 원합니다.

이 시도 : -

당신이 resultDict의 모든 결과를 얻고있다 가정하자.

NSString *long_name = [[[[[resultDict objectForKey:@"results"] objectAtIndex:0] objectForKey:@"address_components"] objectAtIndex:0] objectForKey:@"long_name"]; 

NSLog(@"long_name is %@",long_name); 

참고 : - 당신은 (예 : "I"에 대한) 루프의 변수의와 공을 교체해야 거기에 당신이 결과를 사전에 얻을 배열의 요소 수를 둘 것이다.

관련 문제