2014-02-28 57 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"]);