解析和风天气API的json(objective-C实现)

暑假在家闲着没事干,心血来潮想做个天气预报APP玩玩,听说和风天气API很好,于是注册并试了下,结果它的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点暴露于日光下。"}}}]}

你赢了,我选择死亡

由于奇葩的格式连json解析工具也不能用,好吧我就不厌其烦地层层解析....

注意:{}代表NSDictionary,[]和()代表NSArray。也算是学到了一点

 

-(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 ];
}

 

 

  

 

posted @ 2016-08-16 18:59  阿良良木月火  阅读(2054)  评论(0编辑  收藏  举报