2014-02-28 1 views
-1

앱에 무료 날씨 API를 사용하고 있습니다. 출력에서 정말로 필요한 것은 저온, 예상 고온 및 조건입니다. 내 응용 프로그램에서 NSURLConnection 및 모든 설정하고 잘 작동합니다. 내 코드는 다음과 같습니다iOS에서 API GET 요청 후 JSON 문자열 처리

-(IBAction) weatherView { 




    NSString *bringitalltogether = @"http://api.worldweatheronline.com/free/v1/weather.ashx?q=79201&format=json&key=22gxkzqxwjvzxp44e3wvdp2c"; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:bringitalltogether] 
                  cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60]; 
    [request setHTTPMethod:@"GET"]; 


    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [connection start]; 
    [connection release]; 

} 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"RESPONSE%@", response); 
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) 
    { 
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
     //If you need the response, you can use it here 
     int code = [httpResponse statusCode]; 
     NSLog(@"%i", code); 
     if (code == 200){ 

     } 

     else 
     { 
      UIAlertView *oops = [[UIAlertView alloc] initWithTitle:@"Oops" message:@"The weatherman is having difficulties getting you the forecast. Please check your network settings and try again later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
      [oops show]; 
      [oops release]; 
     } 

    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

    NSMutableDictionary *allResults = [NSJSONSerialization 
             JSONObjectWithData:data 
             options:NSJSONReadingAllowFragments 
             error:nil]; 
    NSLog(@"Data was received%@", allResults.description); 


} 

다, 난 그냥 JSON 문자열 출력과 실제로 필요한 것만 위드 걸릴 여기에서 차례 위치를 모르는 잘 작동하는 것 같다. 내가받을

출력은 다음과 같습니다 당신은 당신의 코드를 게시 한 추론 것에 대해 "모든 것이 잘 작동하는 것 같다"

{ 
    data =  { 
     "current_condition" =   (
         { 
       cloudcover = 0; 
       humidity = 19; 
       "observation_time" = "05:57 PM"; 
       precipMM = "0.0"; 
       pressure = 1005; 
       "temp_C" = 22; 
       "temp_F" = 72; 
       visibility = 16; 
       weatherCode = 113; 
       weatherDesc =     (
             { 
         value = Sunny; 
        } 
       ); 
       weatherIconUrl =     (
             { 
         value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"; 
        } 
       ); 
       winddir16Point = WNW; 
       winddirDegree = 300; 
       windspeedKmph = 35; 
       windspeedMiles = 22; 
      } 
     ); 
     request =   (
         { 
       query = 79201; 
       type = Zipcode; 
      } 
     ); 
     weather =   (
         { 
       date = "2014-02-28"; 
       precipMM = "0.0"; 
       tempMaxC = 25; 
       tempMaxF = 77; 
       tempMinC = 2; 
       tempMinF = 35; 
       weatherCode = 113; 
       weatherDesc =     (
             { 
         value = Sunny; 
        } 
       ); 
       weatherIconUrl =     (
             { 
         value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"; 
        } 
       ); 
       winddir16Point = NW; 
       winddirDegree = 307; 
       winddirection = NW; 
       windspeedKmph = 33; 
       windspeedMiles = 20; 
      } 
     ); 
    }; 
} 
+1

경우? – Avt

+0

무엇이 문제입니까? JSON을 구문 분석하는 방법? – Avt

+1

'allResults'는 사전이므로 키 값을 요청한 적이 있습니까? – Wain

답변

1

여기에 코드

// get data 
NSDictionary *getData = [allResults objectForKey:@"data"]; 
NSArray *currentConditions = [getData objectForKey:@"current_condition"]; 
NSArray *weathers = [getData objectForKey:@"weather"]; 

NSDictionary *currentCondition = [currentConditions objectAtIndex:0]; 
NSLog(@"current temp = %@", [currentCondition objectForKey:@"temp_C"]); 

NSDictionary *weather = [weathers objectAtIndex:0]; 
NSLog(@"hight temp = %@", [weather objectForKey:@"tempMaxC"]); 
NSLog(@"low temp = %@", [weather objectForKey:@"tempMinC"]);