解析和风天气API的json(objective-C实现)
暑假在家闲着没事干,心血来潮想做个天气预报APP玩玩,听说和风天气API很好,于是注册并试了下,结果它的JSON是这样的(拿我的家乡武义为例子)
- {"HeWeather data service 3.0":[{"aqi":{"city":{"aqi":"","pm10":"","pm25":"","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":"","code_n":"","txt_d":"晴","txt_n":"多云"},"date":"2016-08-16","hum":"","pcpn":"0.0","pop":"","pres":"","tmp":{"max":"","min":""},"vis":"","wind":{"deg":"","dir":"无持续风向","sc":"微风","spd":""}},{"astro":{"sr":"05:32","ss":"18:39"},"cond":{"code_d":"","code_n":"","txt_d":"晴","txt_n":"晴"},"date":"2016-08-17","hum":"","pcpn":"0.0","pop":"","pres":"","tmp":{"max":"","min":""},"vis":"","wind":{"deg":"","dir":"东风","sc":"微风","spd":""}},{"astro":{"sr":"05:33","ss":"18:38"},"cond":{"code_d":"","code_n":"","txt_d":"晴","txt_n":"晴"},"date":"2016-08-18","hum":"","pcpn":"0.0","pop":"","pres":"","tmp":{"max":"","min":""},"vis":"","wind":{"deg":"","dir":"东风","sc":"微风","spd":""}},{"astro":{"sr":"05:33","ss":"18:37"},"cond":{"code_d":"","code_n":"","txt_d":"多云","txt_n":"多云"},"date":"2016-08-19","hum":"","pcpn":"0.0","pop":"","pres":"","tmp":{"max":"","min":""},"vis":"","wind":{"deg":"","dir":"东风","sc":"微风","spd":""}},{"astro":{"sr":"05:34","ss":"18:36"},"cond":{"code_d":"","code_n":"","txt_d":"多云","txt_n":"多云"},"date":"2016-08-20","hum":"","pcpn":"0.0","pop":"","pres":"","tmp":{"max":"","min":""},"vis":"","wind":{"deg":"","dir":"东风","sc":"微风","spd":""}},{"astro":{"sr":"05:34","ss":"18:35"},"cond":{"code_d":"","code_n":"","txt_d":"多云","txt_n":"多云"},"date":"2016-08-21","hum":"","pcpn":"0.0","pop":"","pres":"","tmp":{"max":"","min":""},"vis":"","wind":{"deg":"","dir":"东风","sc":"微风","spd":""}},{"astro":{"sr":"05:35","ss":"18:34"},"cond":{"code_d":"","code_n":"","txt_d":"多云","txt_n":"多云"},"date":"2016-08-22","hum":"","pcpn":"0.0","pop":"","pres":"","tmp":{"max":"","min":""},"vis":"","wind":{"deg":"","dir":"东风","sc":"微风","spd":""}}],"hourly_forecast":[{"date":"2016-08-16 19:00","hum":"","pop":"","pres":"","tmp":"","wind":{"deg":"","dir":"东南风","sc":"微风","spd":""}},{"date":"2016-08-16 22:00","hum":"","pop":"","pres":"","tmp":"","wind":{"deg":"","dir":"东南风","sc":"微风","spd":""}}],"now":{"cond":{"code":"","txt":"晴"},"fl":"","hum":"","pcpn":"","pres":"","tmp":"","vis":"","wind":{"deg":"","dir":"东风","sc":"4-5","spd":""}},"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=;day<;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=;
- if([dateString intValue]<) nowTime=;
- else if([dateString intValue]<) nowTime=;
- else if([dateString intValue]<) nowTime=;
- else if([dateString intValue]<) nowTime=;
- else if([dateString intValue]<) nowTime=;
- for(int day=;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:];
- [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 ];
- }
解析和风天气API的json(objective-C实现)的更多相关文章
- Android解析中国天气网的Json数据
在Android开发中.一般的APP都是通过获取server端的数据来更新UI.从server获取到的数据能够是Json.它的数据量要比XML要小,这里解析中国天气网上获取的数据,尽管已经不再更新了. ...
- 常见天气api
1. 心知天气API1.1 免费版:400次/小时,也就是9600次/天.国内城市数据,天气实况,3天预报,6项生活指数.这个API的免费版已经提供了很多年了,应该算最长寿稳定的那批API了……1.2 ...
- [转]真正的中国天气api接口xml,json
转自:http://blog.csdn.net/fancylovejava/article/details/26102635 我只想说现在网上那几个api完全坑爹有木有??? 官方的申请不来有木有,还 ...
- (转)真正的中国天气api接口xml,json(求加精) ...
我只想说现在网上那几个api完全坑爹有木有??? 官方的申请不来有木有,还有收费有木有?? 咱这种菜鸟只能用免费的了!!!! http://m.weather.com.cn/data/101110 ...
- json-lib-2.4-jdk15.jar所需全部JAR包.rar java jsoup解析开彩网api接口json数据实例
json-lib-2.4-jdk15.jar所需全部JAR包.rar java jsoup解析开彩网api接口json数据实例 json-lib-2.4-jdk15.jar所需全部JAR包.rar ...
- 中国天气api接口xml,json
http://m.weather.com.cn/data/101110101.html 大坑有木有??反应慢不说了,还老不更新!! 想贴段代码的,现在又打不 开了(貌似3月4号以后没更新过) ==== ...
- JAVA的免费天气api接口调用示例
step1:选择本文所示例的接口"免费天气api" url:https://www.juhe.cn/docs/api/id/39/aid/87 step2:每个接口都需要传入一个参 ...
- 雅虎天气API调用
雅虎天气API调用: 1.调用方法:http://weather.yahooapis.com/forecastrss?w=2502265&u=c,绿色字体为城市代号,u=c表示取摄氏度. 2. ...
- 和风天气WebApi使用教程
1.首先进入和风天气开发平台,点击右上角的注册进行注册 和风天气开发平台 2.填写注册用的邮箱和密码完成注册,可能还需要手机号,按提示注册完成即可. 3.从和风天气开发平台右上角进入控制台,输入你刚刚 ...
随机推荐
- C++的动态内存分配
- java 性能优化(代码优化)
参考博文: java 性能优化:35 个小细节,让你提升 java 代码的运行效率
- SQL TOP 子句、SQL LIKE 操作符、SQL 通配符
TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. SQL Server 的语法: S ...
- git学习手册
#git学习手册 git: Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.[2] Git 是 Linus Torvalds 为了帮助管理 Linux内核开发而 ...
- 自动化测试selenium----css选择器总结
选择器 示例 描述 * * 匹配任何元素 element DIV 标签选择器,匹配所有使用DIV标签的元素 .class .tips class选择器,匹配所有class属性中包含info的元素 ...
- logstash使用
简介 Logstash是一个接收,处理,转发日志的工具.支持系统日志,webserver日志,错误日志,应用日志,总之包括所有可以抛出来的日志类型.怎么样听起来挺厉害的吧?在一个典型的使用场景下(EL ...
- C#中Object和Json之间的转换
//创建一个类,此类有两个方法 using Newtonsoft.Json;namespace System{ public static class JsonExtension { ...
- 使用RestTemplate发送post请求
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
- iOS JavaScriptCore与H5交互时出现异常提示
在利用JavaScriptCore与H5交互时出现异常提示: This application is modifying the autolayout engine from a background ...
- Spring操作指南-IoC基础环境配置(基于XML)