雅虎天气api解析

时间:2012-09-11 14:06:34

标签: objective-c ios json macos yahoo-weather-api

如何解析我从链接收到的以下JSON的spped风 http://weather.yahooapis.com/forecastjson?w=2502265。我从中获取垃圾值,但正确获取其余值。任何人都可以告诉我如何才能获得风速?

{"units":{"temperature":"F","speed":"mph","distance":"mi","pressure":"in"},"location":{"location_id":"USCA1116","city":"Sunnyvale","state_abbreviation":"CA","country_abbreviation":"US","elevation":82,"latitude":37.39,"longitude":-122.03},"wind":{"speed":0,"direction":"CALM"},"atmosphere":{"humidity":"86","visibility":"10","pressure":"30.21","rising":"falling"},"url":"http:\/\/weather.yahoo.com\/forecast\/USCA1116.html","logo":"http:\/\/l.yimg.com\/a\/i\/us\/nt\/ma\/ma_nws-we_1.gif","astronomy":{"sunrise":"06:27","sunset":"18:11"},"condition":{"text":"Fair","code":"33","image":"http:\/\/l.yimg.com\/a\/i\/us\/we\/52\/33.gif","temperature":49},"forecast":[{"day":"Today","condition":"PM Showers","high_temperature":"64","low_temperature":"47"},{"day":"Tomorrow","condition":"Partly Cloudy","high_temperature":"62","low_temperature":"45"}]}



NSString *linkForWoeid = [NSString stringWithFormat:@"http://where.yahooapis.com/geocode?location=%@,%@&flags=J&gflags=R&appid=zHgnBS4m",latitude,longitude];
         NSURL *woeid = [NSURL URLWithString:linkForWoeid];
         NSData *WoeidData = [NSData dataWithContentsOfURL:woeid];
         if (WoeidData != NULL)
         {
             NSError *woeiderr = nil;
             //NSLog(@"linkForWoeid:%@woeid:%@woeidData:%@",linkForWoeid,woeid,WoeidData);
             NSDictionary *response1=[NSJSONSerialization JSONObjectWithData:WoeidData options:NSJSONReadingMutableContainers error:&woeiderr]; 
             NSDictionary *woeidDict = [[[[response1 objectForKey:@"ResultSet"]objectForKey:@"Results"]objectAtIndex:0]objectForKey:@"woeid"];

             NSString *address=[NSString stringWithFormat:@"http://weather.yahooapis.com/forecastjson?w=%@",woeidDict];
             NSURL *url=[NSURL URLWithString:address];
             NSData *data=[NSData dataWithContentsOfURL:url];
             NSError *eqw=nil;
             if (data != NULL)
             {
                 NSDictionary *response=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&eqw]; 
                 //NSLog(@"response:%@",response);

                 NSString *highTempDict = [[[response objectForKey:@"forecast"]objectAtIndex:0] objectForKey:@"high_temperature"];
                 NSString *temp = [highTempDict stringByAppendingFormat:@" 'F"];
                          NSString *windSpeed = [[response objectForKey:@"wind"] objectForKey:@"speed"];
                          NSLog(@"wind :%@",windSpeed);
                          if (windSpeed == 0)
                          {
                              NSLog(@"insideif");
                          windSpeed = @"0";
                          }

                 NSString *imageView = [[response objectForKey:@"condition"]objectForKey:@"image" ];

1 个答案:

答案 0 :(得分:2)

Hi Rasi请使用NSNumber代替NSString

NSNumber *windSpeed = [[response objectForKey:@"wind"] objectForKey:@"speed"];

JSON一样,它以整数值(不含引号)出现,因此它不是NSString而是NSNumber

您可以将其字符串值设为[windSpeed stringValue];