Question

How can I parse the wind spped for the following JSON that I have received from the link http://weather.yahooapis.com/forecastjson?w=2502265. I am getting a garbage value from it but getting the rest of the values correctly. Can anybody let me know how can I get the wind speed out of it?

{"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" ];
Was it helpful?

Solution

Hi Rasi Please use NSNumber instead of NSString here

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

As in JSON it's coming as integer value (without quotes) so it is not a NSString but NSNumber

And you can get it's string value as [windSpeed stringValue];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top