简单天气应用开发——解析HeWeather JSON
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单天气应用开发——解析HeWeather JSON相关的知识,希望对你有一定的参考价值。
借助和风天气API成功获取到了我想要的天气信息,但在怎么提取出我想要的数据上又遇到了问题。
{"HeWeather data service 3.0":[{"basic":{"city":"余姚","cnty":"中国","id":"CN101210404","lat":"29.948000","lon":"121.172000","update":{"loc":"2016-02-06 14:58","utc":"2016-02-06 06:58"}},"daily_forecast":[{"astro":{"sr":"06:42","ss":"17:36"},"cond":{"code_d":"101","code_n":"100","txt_d":"多云","txt_n":"晴"},"date":"2016-02-06","hum":"44","pcpn":"0.0","pop":"2","pres":"1030","tmp":{"max":"6","min":"-2"},"vis":"10","wind":{"deg":"324","dir":"西北风","sc":"3-4","spd":"12"}},{"astro":{"sr":"06:41","ss":"17:36"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2016-02-07","hum":"31","pcpn":"0.0","pop":"0","pres":"1024","tmp":{"max":"9","min":"-2"},"vis":"10","wind":{"deg":"282","dir":"西北风","sc":"3-4","spd":"12"}},{"astro":{"sr":"06:41","ss":"17:37"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2016-02-08","hum":"32","pcpn":"0.0","pop":"0","pres":"1024","tmp":{"max":"14","min":"1"},"vis":"10","wind":{"deg":"274","dir":"西风","sc":"微风","spd":"6"}},{"astro":{"sr":"06:40","ss":"17:38"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2016-02-09","hum":"30","pcpn":"0.0","pop":"0","pres":"1023","tmp":{"max":"16","min":"6"},"vis":"10","wind":{"deg":"95","dir":"南风","sc":"3-4","spd":"12"}},{"astro":{"sr":"06:39","ss":"17:39"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2016-02-10","hum":"51","pcpn":"0.0","pop":"0","pres":"1020","tmp":{"max":"18","min":"8"},"vis":"10","wind":{"deg":"167","dir":"东南风","sc":"3-4","spd":"10"}},{"astro":{"sr":"06:38","ss":"17:40"},"cond":{"code_d":"300","code_n":"101","txt_d":"阵雨","txt_n":"多云"},"date":"2016-02-11","hum":"60","pcpn":"0.3","pop":"51","pres":"1015","tmp":{"max":"19","min":"10"},"vis":"10","wind":{"deg":"177","dir":"南风","sc":"微风","spd":"9"}},{"astro":{"sr":"06:38","ss":"17:40"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2016-02-12","hum":"54","pcpn":"7.0","pop":"50","pres":"1010","tmp":{"max":"20","min":"11"},"vis":"10","wind":{"deg":"241","dir":"南风","sc":"3-4","spd":"11"}}],"hourly_forecast":[{"date":"2016-02-06 16:00","hum":"46","pop":"1","pres":"1030","tmp":"5","wind":{"deg":"326","dir":"西北风","sc":"3-4","spd":"26"}},{"date":"2016-02-06 19:00","hum":"50","pop":"0","pres":"1031","tmp":"3","wind":{"deg":"325","dir":"西北风","sc":"3-4","spd":"19"}},{"date":"2016-02-06 22:00","hum":"55","pop":"0","pres":"1031","tmp":"2","wind":{"deg":"308","dir":"西北风","sc":"微风","spd":"14"}}],"now":{"cond":{"code":"101","txt":"多云"},"fl":"3","hum":"54","pcpn":"0","pres":"1029","tmp":"3","vis":"6","wind":{"deg":"330","dir":"西风","sc":"5-6","spd":"30"}},"status":"ok","suggestion":{"comf":{"brf":"较不舒适","txt":"白天天气晴好,但仍会使您感觉偏冷,不很舒适,请注意适时添加衣物,以防感冒。"},"cw":{"brf":"较适宜","txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},"drsg":{"brf":"冷","txt":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。"},"flu":{"brf":"易发","txt":"昼夜温差很大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"},"sport":{"brf":"较不宜","txt":"天气较好,但考虑天气寒冷,风力较强,推荐您进行室内运动,若户外运动请注意保暖并做好准备活动。"},"trav":{"brf":"一般","txt":"天空状况还是比较好的,但温度稍微有点低,且风稍大,会让您感觉些许凉意。外出请注意防风。"},"uv":{"brf":"最弱","txt":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}}}]}
这串JSON和我以前学习JSON解析时的完全不一样,字典和数组一层套一层,看得头都晕了。还好有JSON在线视图查看器,导入后可以清晰的看到层次结构:
现在就好办多了,只要一层层地把数据剥出来就OK
NSArray *preData = dict[ @"HeWeather data service 3.0" ];NSDictionary *weatherData = preData[0];NSDictionary *basic = weatherData[@"basic" ];NSArray *dailyForecast = weatherData[@"daily_forecast" ];NSDictionary *now = weatherData[@"now" ];
以上是关于简单天气应用开发——解析HeWeather JSON的主要内容,如果未能解决你的问题,请参考以下文章