0

如何解析从链接收到的以下 JSON 的风速 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" ];
4

1 回答 1

2

嗨 Rasi 请使用这里NSNumber而不是NSString这里

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

因为JSON它以整数值(不带引号)的形式出现,所以它不是NSString但是NSNumber

你可以得到它的字符串值[windSpeed stringValue];

于 2012-09-11T14:37:07.930 回答