博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单天气应用开发——解析HeWeather JSON
阅读量:6302 次
发布时间:2019-06-22

本文共 3578 字,大约阅读时间需要 11 分钟。

  借助和风天气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解析时的完全不一样,字典和数组一层套一层,看得头都晕了。还好有,导入后可以清晰的看到层次结构:

  现在就好办多了,只要一层层地把数据剥出来就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" ];
 

转载于:https://www.cnblogs.com/fallinDeepSea/p/5186460.html

你可能感兴趣的文章
(图解)使用SourceTree将bitbucket的远程仓库回滚到某一次提交
查看>>
Navicat 2003-Can't connect to MySQL server on 'localhost'(10061)
查看>>
XenServer 6安装体验
查看>>
linux Centos7下安装python3及pip3
查看>>
分享25个高质量的移动设备wordpress主题(Mobile theme)
查看>>
进入LINUX后显示-bash-3.2的解决办法以及root下的shell切换
查看>>
从我做起 拒绝滥用“低格”
查看>>
关于进程和系统服务的控制(七八章),本章没有详细说明top命令
查看>>
SpringMVC+Mybatis架构下的增删查改
查看>>
php引用(&)
查看>>
scapy项目实战
查看>>
Python之encode与decode浅析
查看>>
家庭无线网络组网
查看>>
网站万能密码大全
查看>>
我和儿子一起做的手工飞机和枪
查看>>
sqlite3 多线程问题..
查看>>
二维随机变量及其分布
查看>>
Centos安装zabbix监控
查看>>
Kettle学习笔记(三)
查看>>
shell案例 - 你最喜欢敲的十大命令 ^ v ^
查看>>