雅虎天气api解析

问题描述:

如何解析我从链接 http://weather.yahooapis.com/forecastjson?w=2502265收到的以下JSON。我从中得到一个垃圾值,但正确地获取其余的值。任何人都可以让我知道我怎样才能从中获得风速?雅虎天气api解析

{"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" ]; 
+0

你将不得不成为一个littl e更具体。你现在在做什么?你的结果是什么?这是什么垃圾价值? – GarlicFries

+0

具体我得到0或有时在负面。 –

+0

这并不是** **。告诉我们你的代码。好悲伤 - 我们甚至不知道你在用什么语言! – GarlicFries

拉西嗨请使用NSNumber代替NSString这里

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

正如JSON它的到来为整数值(不带引号),所以它不是一个NSStringNSNumber

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