暑假在家閒着沒事幹,心血來潮想作個天氣預報APP玩玩,據說和風天氣API很好,因而註冊並試了下,結果它的JSON是這樣的(拿個人家鄉武義爲例子)json
{"HeWeather data service 3.0":[{"aqi":{"city":{"aqi":"73","pm10":"54","pm25":"39","qlty":"良"}},"basic":{"city":"武義","cnty":"中國","id":"CN101210906","lat":"28.530000","lon":"119.480000","update":{"loc":"2016-08-16 17:52","utc":"2016-08-16 09:52"}},"daily_forecast":[{"astro":{"sr":"05:32","ss":"18:40"},"cond":{"code_d":"100","code_n":"101","txt_d":"晴","txt_n":"多雲"},"date":"2016-08-16","hum":"54","pcpn":"0.0","pop":"0","pres":"1001","tmp":{"max":"38","min":"27"},"vis":"10","wind":{"deg":"121","dir":"無持續風向","sc":"微風","spd":"3"}},{"astro":{"sr":"05:32","ss":"18:39"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2016-08-17","hum":"54","pcpn":"0.0","pop":"1","pres":"1000","tmp":{"max":"38","min":"27"},"vis":"10","wind":{"deg":"123","dir":"東風","sc":"微風","spd":"10"}},{"astro":{"sr":"05:33","ss":"18:38"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2016-08-18","hum":"55","pcpn":"0.0","pop":"1","pres":"1000","tmp":{"max":"38","min":"28"},"vis":"10","wind":{"deg":"148","dir":"東風","sc":"微風","spd":"3"}},{"astro":{"sr":"05:33","ss":"18:37"},"cond":{"code_d":"101","code_n":"101","txt_d":"多雲","txt_n":"多雲"},"date":"2016-08-19","hum":"56","pcpn":"0.0","pop":"0","pres":"1000","tmp":{"max":"38","min":"29"},"vis":"10","wind":{"deg":"158","dir":"東風","sc":"微風","spd":"8"}},{"astro":{"sr":"05:34","ss":"18:36"},"cond":{"code_d":"101","code_n":"101","txt_d":"多雲","txt_n":"多雲"},"date":"2016-08-20","hum":"49","pcpn":"0.0","pop":"8","pres":"1001","tmp":{"max":"37","min":"27"},"vis":"10","wind":{"deg":"138","dir":"東風","sc":"微風","spd":"0"}},{"astro":{"sr":"05:34","ss":"18:35"},"cond":{"code_d":"101","code_n":"101","txt_d":"多雲","txt_n":"多雲"},"date":"2016-08-21","hum":"46","pcpn":"0.0","pop":"0","pres":"1002","tmp":{"max":"36","min":"26"},"vis":"10","wind":{"deg":"100","dir":"東風","sc":"微風","spd":"4"}},{"astro":{"sr":"05:35","ss":"18:34"},"cond":{"code_d":"101","code_n":"101","txt_d":"多雲","txt_n":"多雲"},"date":"2016-08-22","hum":"45","pcpn":"0.0","pop":"0","pres":"1003","tmp":{"max":"35","min":"25"},"vis":"10","wind":{"deg":"89","dir":"東風","sc":"微風","spd":"0"}}],"hourly_forecast":[{"date":"2016-08-16 19:00","hum":"72","pop":"0","pres":"1001","tmp":"36","wind":{"deg":"136","dir":"東南風","sc":"微風","spd":"7"}},{"date":"2016-08-16 22:00","hum":"83","pop":"0","pres":"1003","tmp":"34","wind":{"deg":"142","dir":"東南風","sc":"微風","spd":"8"}}],"now":{"cond":{"code":"100","txt":"晴"},"fl":"44","hum":"43","pcpn":"0","pres":"1000","tmp":"36","vis":"10","wind":{"deg":"128","dir":"東風","sc":"4-5","spd":"20"}},"status":"ok","suggestion":{"comf":{"brf":"極不溫馨","txt":"白每天氣晴好、炎熱,您會感到酷熱難耐,極爲不適,請注意防暑降溫。"},"cw":{"brf":"較適宜","txt":"較適宜洗車,將來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。"},"drsg":{"brf":"炎熱","txt":"天氣炎熱,建議着短衫、短裙、短褲、薄型T恤衫等清涼夏季服裝。"},"flu":{"brf":"少發","txt":"各項氣象條件適宜,發生感冒機率較低。但請避免長期處於空調房間中,以防感冒。"},"sport":{"brf":"較適宜","txt":"天氣較好,戶外運動請注意防曬。推薦您進行室內運動。"},"trav":{"brf":"較不宜","txt":"天氣較好,溫度比較高,中午時段儘可能不要外出,如外出可選擇水上娛樂等清涼項目。並請注意防暑和防曬。"},"uv":{"brf":"強","txt":"紫外線輻射強,建議塗擦SPF20左右、PA++的防曬護膚品。避免在10點至14點暴露於日光下。"}}}]}
你贏了,我選擇死亡api
因爲奇葩的格式連json解析工具也不能用,好吧我就不厭其煩地層層解析....工具
注意:{}表明NSDictionary,[]和()表明NSArray。也算是學到了一點url
-(void)requestForApi:(NSDictionary*)jsonDict{//接收json中的api內容 NSDictionary *wyCity = [[jsonDict objectForKey:@"aqi"] objectForKey:@"city"]; NSString *wyAqiStr = [wyCity objectForKey:@"aqi"]; NSString *wyPm10 = [wyCity objectForKey:@"pm10"]; NSString *wyPm25 = [wyCity objectForKey:@"pm25"]; NSString *wyQlty = [wyCity objectForKey:@"qlty"]; } -(void)requestForBasic:(NSDictionary*)jsonDict{ NSDictionary *wyBasic = [jsonDict objectForKey:@"basic"]; NSString *wyCity = [wyBasic objectForKey:@"city"]; NSString *wyCnty = [wyBasic objectForKey:@"cnty"]; NSString *wyId = [wyBasic objectForKey:@"id"]; NSString *wyLat = [wyBasic objectForKey:@"lat"]; NSString *wyLon = [wyBasic objectForKey:@"lon"]; NSDictionary *wyUpdate= [wyBasic objectForKey:@"update"]; [self requestForUpdate:wyUpdate]; } -(void)requestForUpdate:(NSDictionary*)jsonUpdate{ NSString *wyLoc = [jsonUpdate objectForKey:@"loc"]; NSString *myUtc = [jsonUpdate objectForKey:@"utc"]; } -(void)requestForAstro:(NSDictionary*)jsonAstro{ NSString *wySr = [jsonAstro objectForKey:@"sr"]; NSString *wySs = [jsonAstro objectForKey:@"ss"]; } -(void)requestForCond:(NSDictionary*)jsonCond{ NSString *wyCodeD = [jsonCond objectForKey:@"code_d"]; NSString *wyCodeN = [jsonCond objectForKey:@"code_n"]; NSString *wyTxtD = [jsonCond objectForKey:@"txt_d"]; NSString *wyTxtN = [jsonCond objectForKey:@"txt_n"]; } -(void)requestForNowCond:(NSDictionary*)jsonNowCond{ NSString *wyCode = [jsonNowCond objectForKey:@"code"]; NSString *wyTxt = [jsonNowCond objectForKey:@"txt"]; } -(void)requestForTmp:(NSDictionary*)jsonTmp{ NSString *wyMax = [jsonTmp objectForKey:@"max"]; NSString *wyMin = [jsonTmp objectForKey:@"min"]; } -(void)requestForWind:(NSDictionary*)jsonWind{ NSString *wyDeg = [jsonWind objectForKey:@"deg"]; NSString *wyDir = [jsonWind objectForKey:@"dir"]; NSString *wySc = [jsonWind objectForKey:@"sc"]; NSString *wySpd = [jsonWind objectForKey:@"spd"]; } -(void)requestForNowWind:(NSDictionary*)jsonNowWind{ NSString *wyDeg = [jsonNowWind objectForKey:@"deg"]; NSString *wyDir = [jsonNowWind objectForKey:@"dir"]; NSString *wySc = [jsonNowWind objectForKey:@"sc"]; NSString *wySpd = [jsonNowWind objectForKey:@"spd"]; } -(void)requestForHourlyWind:(NSDictionary*)jsonHourlyWind{ NSString *wyDeg = [jsonHourlyWind objectForKey:@"deg"]; NSString *wyDir = [jsonHourlyWind objectForKey:@"dir"]; NSString *wySc = [jsonHourlyWind objectForKey:@"sc"]; NSString *wySpd = [jsonHourlyWind objectForKey:@"spd"]; } -(void)requestForDailyForecast:(NSDictionary*)jsonDailyForecast{ NSArray *wyDailyForecast = [jsonDailyForecast objectForKey:@"daily_forecast"]; NSDictionary *wyDay = [[NSDictionary alloc]init]; NSDictionary *wyAstro = [[NSDictionary alloc]init]; NSDictionary *wyCond = [[NSDictionary alloc]init]; NSString *wyDate = [[NSString alloc]init]; NSString *wyHum = [[NSString alloc]init]; NSString *wyPcpn = [[NSString alloc]init]; NSString *wyPop = [[NSString alloc]init]; NSString *wyPres = [[NSString alloc]init]; NSDictionary *wyTmp = [[NSDictionary alloc]init]; NSString *wyVis = [[NSString alloc]init]; NSDictionary *wyWind = [[NSDictionary alloc]init]; for(int day=0;day<7;day++){ wyDay = [wyDailyForecast objectAtIndex:day]; wyAstro = [wyDay objectForKey:@"astro"]; [self requestForAstro:wyAstro]; wyCond = [wyDay objectForKey:@"cond"]; [self requestForCond:wyCond]; wyDate = [wyDay objectForKey:@"date"]; wyHum = [wyDay objectForKey:@"hum"]; wyPcpn = [wyDay objectForKey:@"pcpn"]; wyPop = [wyDay objectForKey:@"pop"]; wyPres = [wyDay objectForKey:@"pres"]; wyTmp = [wyDay objectForKey:@"tmp"]; [self requestForTmp:wyTmp]; wyVis = [wyDay objectForKey:@"vis"]; wyWind = [wyDay objectForKey:@"wind"]; [self requestForWind:wyWind]; } } -(void)requestForHourlyForecast:(NSDictionary*)jsonHourlyForecast{ NSArray *wyHourlyForecast = [jsonHourlyForecast objectForKey:@"hourly_forecast"]; NSDictionary *wyDay = [[NSDictionary alloc]init]; NSString *wyDate = [[NSString alloc]init]; NSString *wyHum = [[NSString alloc]init]; NSString *wyPop = [[NSString alloc]init]; NSString *wyPres = [[NSString alloc]init]; NSString *wyTmp = [[NSString alloc]init]; NSDictionary *wyWind = [[NSDictionary alloc]init]; NSDate *currentDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH"]; NSString *dateString = [dateFormatter stringFromDate:currentDate]; int nowTime=0; if([dateString intValue]<10) nowTime=5; else if([dateString intValue]<13) nowTime=4; else if([dateString intValue]<16) nowTime=3; else if([dateString intValue]<19) nowTime=2; else if([dateString intValue]<22) nowTime=1; for(int day=0;day<nowTime;day++){ wyDay = [wyHourlyForecast objectAtIndex:day]; wyDate = [wyDay objectForKey:@"date"]; wyHum = [wyDay objectForKey:@"hum"]; wyPop = [wyDay objectForKey:@"pop"]; wyPres = [wyDay objectForKey:@"pres"]; wyTmp = [wyDay objectForKey:@"tmp"]; wyWind = [wyDay objectForKey:@"wind"]; [self requestForHourlyWind:wyWind]; } } -(void)requestForNow:(NSDictionary*)jsonNow{ NSDictionary *wyNow = [jsonNow objectForKey:@"now"]; NSDictionary *wyCond = [wyNow objectForKey:@"cond"]; NSString *wyFl = [wyNow objectForKey:@"fl"]; NSString *wyHum = [wyNow objectForKey:@"hum"]; NSString *wyPcpn = [wyNow objectForKey:@"pcpn"]; NSString *wyPres = [wyNow objectForKey:@"pres"]; NSString *wyTmp= [wyNow objectForKey:@"tmp"]; _weatherTemperature.text=wyTmp; //NSLog(@"%@",self.weatherTemperature.text); NSDictionary *wyWind = [wyNow objectForKey:@"wind"]; [self requestForNowCond:wyCond]; [self requestForNowWind:wyWind]; } -(void)requestForComf:(NSDictionary*)jsonComf{ NSString *wyBrf = [jsonComf objectForKey:@"brf"]; NSString *wyTxt = [jsonComf objectForKey:@"txt"]; } -(void)requestForCw:(NSDictionary*)jsonCw{ NSString *wyBrf = [jsonCw objectForKey:@"brf"]; NSString *wyTxt = [jsonCw objectForKey:@"txt"]; } -(void)requestForDrsg:(NSDictionary*)jsonDrsg{ NSString *wyBrf = [jsonDrsg objectForKey:@"brf"]; NSString *wyTxt = [jsonDrsg objectForKey:@"txt"]; } -(void)requestForFlu:(NSDictionary*)jsonFlu{ NSString *wyBrf = [jsonFlu objectForKey:@"brf"]; NSString *wyTxt = [jsonFlu objectForKey:@"txt"]; } -(void)requestForSport:(NSDictionary*)jsonSport{ NSString *wyBrf = [jsonSport objectForKey:@"brf"]; NSString *wyTxt = [jsonSport objectForKey:@"txt"]; } -(void)requestForTrav:(NSDictionary*)jsonTrav{ NSString *wyBrf = [jsonTrav objectForKey:@"brf"]; NSString *wyTxt = [jsonTrav objectForKey:@"txt"]; } -(void)requestForUv:(NSDictionary*)jsonUv{ NSString *wyBrf = [jsonUv objectForKey:@"brf"]; NSString *wyTxt = [jsonUv objectForKey:@"txt"]; } -(void)requestForSuggestion:(NSDictionary*)jsonSuggestion{ NSDictionary *wySuggestion = [jsonSuggestion objectForKey:@"suggestion"]; NSDictionary *wyComf = [wySuggestion objectForKey:@"comf"]; [self requestForComf:wyComf]; NSDictionary *wyCw = [wySuggestion objectForKey:@"cw"]; [self requestForCw:wyCw]; NSDictionary *wyDrsg = [wySuggestion objectForKey:@"drsg"]; [self requestForDrsg:wyDrsg]; NSDictionary *wyFlu = [wySuggestion objectForKey:@"flu"]; [self requestForFlu:wyFlu]; NSDictionary *wySport = [wySuggestion objectForKey:@"sport"]; [self requestForSport:wySport]; NSDictionary *wyTrav = [wySuggestion objectForKey:@"trav"]; [self requestForTrav:wyTrav]; NSDictionary *wyUv = [wySuggestion objectForKey:@"uv"]; [self requestForUv:wyUv]; } -(void)request: (NSString*)httpUrl { NSError *error; //加載一個NSURL對象 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:httpUrl]]; //將請求的url數據放到NSData對象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自帶解析類NSJSONSerialization從response中解析出數據放到字典中 NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSArray *jsonArr = [jsonDict objectForKey:@"HeWeather data service 3.0"]; NSDictionary *jsonDictionary = [jsonArr objectAtIndex:0]; [self requestForApi:jsonDictionary]; [self requestForBasic:jsonDictionary]; [self requestForDailyForecast:jsonDictionary]; [self requestForHourlyForecast:jsonDictionary]; [self requestForNow:jsonDictionary]; NSString *wyStatus = [[jsonDictionary objectForKey:@"aqi"] objectForKey:@"ok"]; [self requestForSuggestion:jsonDictionary]; } - (void)viewDidLoad { [super viewDidLoad]; NSString *httpUrl = @"https://api.heweather.com/x3/weather?cityid=CN101210906&key=1cce17d4649344949ef7a619efec1cd5"; [self request: httpUrl ]; }