2013-05-07 4 views
0

아래 URL에서 지정된 데이터 html_instructions을 가져 오려고합니다. 나는이 모든 데이터를 URL에서 가져갈 수 있습니다.NSData에서 세부 정보를 가져 오는 방법

하지만 구체적으로 html_instructions이 필요합니다. 어떻게 데이터를 분할 할 수 있습니까?

나는있는 NSData에 URL을 변환하려면 다음 코드를 사용 :

NSString *url = [NSString  stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=Chennai&destination=Madurai&sensor=false"]; 
    NSURL *googleRequestURL=[NSURL URLWithString:url]; 
    dispatch_async(kBgQueue, ^{ 
    NSData* data = [NSData dataWithContentsOfURL: googleRequestURL]; 
    }); 

답변

1

나는 다음의 URL에서 JSON을 구문 분석 HTML 지침이 코드를 복사하려고 시도 인쇄 : http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false

NSData *receivedData = Received data from url; 
    NSError* error; 
    NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:receiveData    options:kNilOptions error:&error]; 

      NSArray *allkeys = [parsedJson allKeys]; 

     for(int i = 0; i < allkeys.count; i++){ 
      NSLog(@"############################"); 
      if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){ 
       NSArray *arr  = [responseJsonValue objectForKey:@"routes"]; 
       NSDictionary *dic = [arr objectAtIndex:0]; 
       NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]); 
       NSArray *legs = [dic objectForKey:@"legs"]; 
        NSLog(@"legs array count %d", legs.count); 
       for(int i = 0; i < legs.count; i++){ 
        NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"]; 
        for (int i = 0; i < stepsArr.count; i++) { 
         NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]); 
        } 
       } 

      } 
      NSLog(@"############################"); 
     } 
+0

에서 "html_instructions"데이터 만 지정하면 방해받을 수있어 죄송합니다. 나는 "YOURKEY"장소에서 "html_instructions"를주었습니다. 그러나 null을 표시합니다. –

+0

parsedJson을 게시 할 수 있습니까? 어느 쪽이 null입니까? – jailani

+0

네,하지만 parsedjson을 게시하지만 그것은 null만을 보여줍니다. –

2

사용이 같이 당신을하는 데 도움이

NSData* data = [NSData dataWithContentsOfURL:googleRequestURL]; 
    if (data == nil) { 
     return; 
    } 
    NSError* error; 
    NSMutableDictionary* json = [NSJSONSerialization 
           JSONObjectWithData:data 

           options:kNilOptions 
           error:&error]; 

    NSLog(@"Json : %@",json); 

를 바랍니다.

+0

+1 니스 초고속 통합의 대답을! :) – rptwsthi

+0

대담한 nishant에게 감사드립니다. Json은 URL의 모든 데이터도 표시합니다. 필요에 따라 URl.thanks –

관련 문제