Objective c yahoo天气api解析
如何解析从链接接收到的以下JSON的wind sppedObjective c yahoo天气api解析,objective-c,ios,json,macos,yahoo-weather-api,Objective C,Ios,Json,Macos,Yahoo Weather Api,如何解析从链接接收到的以下JSON的wind spped http://weather.yahooapis.com/forecastjson?w=2502265。我从中得到一个垃圾值,但是正确地得到了其余的值。有人能告诉我怎样才能得到风速吗 {"units":{"temperature":"F","speed":"mph","distance":"mi","pressure":"in"},"location":{"location_id":"USCA1116","city":"Sunnyvale
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" ];
Hi-Rasi请在此处使用
NSNumber
而不是NSString
NSNumber *windSpeed = [[response objectForKey:@"wind"] objectForKey:@"speed"];
正如在JSON
中一样,它是一个整数值(不带引号),因此它不是NSString
,而是NSNumber
您可以将其字符串值作为
[windSpeed stringValue]代码>您必须更具体一点。你现在想干什么?你的结果是什么?这个垃圾值是多少?具体来说,我得到的是0,或者有时是负数。这一点都不具体。给我们看看你的代码。天哪,我们都不知道你在说什么!我只是想知道JSON解析中的风速值没有双引号。这是导致问题的原因吗?对不起,信息不完整。希望您现在能正确理解。