暑假在家闲着没事干,心血来潮想做个天气预报APP玩玩,听说和风天气API很好,于是注册并试了下,结果它的JSON是这样的(拿我的家乡武义为例子)

  1. {"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。也算是学到了一点

  1. -(void)requestForApi:(NSDictionary*)jsonDict{//接收json中的api内容
  2. NSDictionary *wyCity = [[jsonDict objectForKey:@"aqi"] objectForKey:@"city"];
  3. NSString *wyAqiStr = [wyCity objectForKey:@"aqi"];
  4. NSString *wyPm10 = [wyCity objectForKey:@"pm10"];
  5. NSString *wyPm25 = [wyCity objectForKey:@"pm25"];
  6. NSString *wyQlty = [wyCity objectForKey:@"qlty"];
  7. }
  8. -(void)requestForBasic:(NSDictionary*)jsonDict{
  9. NSDictionary *wyBasic = [jsonDict objectForKey:@"basic"];
  10. NSString *wyCity = [wyBasic objectForKey:@"city"];
  11. NSString *wyCnty = [wyBasic objectForKey:@"cnty"];
  12. NSString *wyId = [wyBasic objectForKey:@"id"];
  13. NSString *wyLat = [wyBasic objectForKey:@"lat"];
  14. NSString *wyLon = [wyBasic objectForKey:@"lon"];
  15. NSDictionary *wyUpdate= [wyBasic objectForKey:@"update"];
  16. [self requestForUpdate:wyUpdate];
  17. }
  18. -(void)requestForUpdate:(NSDictionary*)jsonUpdate{
  19. NSString *wyLoc = [jsonUpdate objectForKey:@"loc"];
  20. NSString *myUtc = [jsonUpdate objectForKey:@"utc"];
  21. }
  22. -(void)requestForAstro:(NSDictionary*)jsonAstro{
  23. NSString *wySr = [jsonAstro objectForKey:@"sr"];
  24. NSString *wySs = [jsonAstro objectForKey:@"ss"];
  25. }
  26. -(void)requestForCond:(NSDictionary*)jsonCond{
  27. NSString *wyCodeD = [jsonCond objectForKey:@"code_d"];
  28. NSString *wyCodeN = [jsonCond objectForKey:@"code_n"];
  29. NSString *wyTxtD = [jsonCond objectForKey:@"txt_d"];
  30. NSString *wyTxtN = [jsonCond objectForKey:@"txt_n"];
  31. }
  32. -(void)requestForNowCond:(NSDictionary*)jsonNowCond{
  33. NSString *wyCode = [jsonNowCond objectForKey:@"code"];
  34. NSString *wyTxt = [jsonNowCond objectForKey:@"txt"];
  35. }
  36. -(void)requestForTmp:(NSDictionary*)jsonTmp{
  37. NSString *wyMax = [jsonTmp objectForKey:@"max"];
  38. NSString *wyMin = [jsonTmp objectForKey:@"min"];
  39. }
  40. -(void)requestForWind:(NSDictionary*)jsonWind{
  41. NSString *wyDeg = [jsonWind objectForKey:@"deg"];
  42. NSString *wyDir = [jsonWind objectForKey:@"dir"];
  43. NSString *wySc = [jsonWind objectForKey:@"sc"];
  44. NSString *wySpd = [jsonWind objectForKey:@"spd"];
  45. }
  46. -(void)requestForNowWind:(NSDictionary*)jsonNowWind{
  47. NSString *wyDeg = [jsonNowWind objectForKey:@"deg"];
  48. NSString *wyDir = [jsonNowWind objectForKey:@"dir"];
  49. NSString *wySc = [jsonNowWind objectForKey:@"sc"];
  50. NSString *wySpd = [jsonNowWind objectForKey:@"spd"];
  51. }
  52. -(void)requestForHourlyWind:(NSDictionary*)jsonHourlyWind{
  53. NSString *wyDeg = [jsonHourlyWind objectForKey:@"deg"];
  54. NSString *wyDir = [jsonHourlyWind objectForKey:@"dir"];
  55. NSString *wySc = [jsonHourlyWind objectForKey:@"sc"];
  56. NSString *wySpd = [jsonHourlyWind objectForKey:@"spd"];
  57. }
  58. -(void)requestForDailyForecast:(NSDictionary*)jsonDailyForecast{
  59. NSArray *wyDailyForecast = [jsonDailyForecast objectForKey:@"daily_forecast"];
  60. NSDictionary *wyDay = [[NSDictionary alloc]init];
  61. NSDictionary *wyAstro = [[NSDictionary alloc]init];
  62. NSDictionary *wyCond = [[NSDictionary alloc]init];
  63. NSString *wyDate = [[NSString alloc]init];
  64. NSString *wyHum = [[NSString alloc]init];
  65. NSString *wyPcpn = [[NSString alloc]init];
  66. NSString *wyPop = [[NSString alloc]init];
  67. NSString *wyPres = [[NSString alloc]init];
  68. NSDictionary *wyTmp = [[NSDictionary alloc]init];
  69. NSString *wyVis = [[NSString alloc]init];
  70. NSDictionary *wyWind = [[NSDictionary alloc]init];
  71. for(int day=;day<;day++){
  72. wyDay = [wyDailyForecast objectAtIndex:day];
  73. wyAstro = [wyDay objectForKey:@"astro"];
  74. [self requestForAstro:wyAstro];
  75. wyCond = [wyDay objectForKey:@"cond"];
  76. [self requestForCond:wyCond];
  77. wyDate = [wyDay objectForKey:@"date"];
  78. wyHum = [wyDay objectForKey:@"hum"];
  79. wyPcpn = [wyDay objectForKey:@"pcpn"];
  80. wyPop = [wyDay objectForKey:@"pop"];
  81. wyPres = [wyDay objectForKey:@"pres"];
  82. wyTmp = [wyDay objectForKey:@"tmp"];
  83. [self requestForTmp:wyTmp];
  84. wyVis = [wyDay objectForKey:@"vis"];
  85. wyWind = [wyDay objectForKey:@"wind"];
  86. [self requestForWind:wyWind];
  87. }
  88. }
  89. -(void)requestForHourlyForecast:(NSDictionary*)jsonHourlyForecast{
  90. NSArray *wyHourlyForecast = [jsonHourlyForecast objectForKey:@"hourly_forecast"];
  91. NSDictionary *wyDay = [[NSDictionary alloc]init];
  92. NSString *wyDate = [[NSString alloc]init];
  93. NSString *wyHum = [[NSString alloc]init];
  94. NSString *wyPop = [[NSString alloc]init];
  95. NSString *wyPres = [[NSString alloc]init];
  96. NSString *wyTmp = [[NSString alloc]init];
  97. NSDictionary *wyWind = [[NSDictionary alloc]init];
  98. NSDate *currentDate = [NSDate date];
  99. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  100. [dateFormatter setDateFormat:@"HH"];
  101. NSString *dateString = [dateFormatter stringFromDate:currentDate];
  102. int nowTime=;
  103. if([dateString intValue]<) nowTime=;
  104. else if([dateString intValue]<) nowTime=;
  105. else if([dateString intValue]<) nowTime=;
  106. else if([dateString intValue]<) nowTime=;
  107. else if([dateString intValue]<) nowTime=;
  108. for(int day=;day<nowTime;day++){
  109. wyDay = [wyHourlyForecast objectAtIndex:day];
  110. wyDate = [wyDay objectForKey:@"date"];
  111. wyHum = [wyDay objectForKey:@"hum"];
  112. wyPop = [wyDay objectForKey:@"pop"];
  113. wyPres = [wyDay objectForKey:@"pres"];
  114. wyTmp = [wyDay objectForKey:@"tmp"];
  115. wyWind = [wyDay objectForKey:@"wind"];
  116. [self requestForHourlyWind:wyWind];
  117. }
  118. }
  119. -(void)requestForNow:(NSDictionary*)jsonNow{
  120. NSDictionary *wyNow = [jsonNow objectForKey:@"now"];
  121. NSDictionary *wyCond = [wyNow objectForKey:@"cond"];
  122. NSString *wyFl = [wyNow objectForKey:@"fl"];
  123. NSString *wyHum = [wyNow objectForKey:@"hum"];
  124. NSString *wyPcpn = [wyNow objectForKey:@"pcpn"];
  125. NSString *wyPres = [wyNow objectForKey:@"pres"];
  126. NSString *wyTmp= [wyNow objectForKey:@"tmp"];
  127. _weatherTemperature.text=wyTmp;
  128. //NSLog(@"%@",self.weatherTemperature.text);
  129. NSDictionary *wyWind = [wyNow objectForKey:@"wind"];
  130. [self requestForNowCond:wyCond];
  131. [self requestForNowWind:wyWind];
  132. }
  133. -(void)requestForComf:(NSDictionary*)jsonComf{
  134. NSString *wyBrf = [jsonComf objectForKey:@"brf"];
  135. NSString *wyTxt = [jsonComf objectForKey:@"txt"];
  136. }
  137. -(void)requestForCw:(NSDictionary*)jsonCw{
  138. NSString *wyBrf = [jsonCw objectForKey:@"brf"];
  139. NSString *wyTxt = [jsonCw objectForKey:@"txt"];
  140. }
  141. -(void)requestForDrsg:(NSDictionary*)jsonDrsg{
  142. NSString *wyBrf = [jsonDrsg objectForKey:@"brf"];
  143. NSString *wyTxt = [jsonDrsg objectForKey:@"txt"];
  144. }
  145. -(void)requestForFlu:(NSDictionary*)jsonFlu{
  146. NSString *wyBrf = [jsonFlu objectForKey:@"brf"];
  147. NSString *wyTxt = [jsonFlu objectForKey:@"txt"];
  148. }
  149. -(void)requestForSport:(NSDictionary*)jsonSport{
  150. NSString *wyBrf = [jsonSport objectForKey:@"brf"];
  151. NSString *wyTxt = [jsonSport objectForKey:@"txt"];
  152. }
  153. -(void)requestForTrav:(NSDictionary*)jsonTrav{
  154. NSString *wyBrf = [jsonTrav objectForKey:@"brf"];
  155. NSString *wyTxt = [jsonTrav objectForKey:@"txt"];
  156. }
  157. -(void)requestForUv:(NSDictionary*)jsonUv{
  158. NSString *wyBrf = [jsonUv objectForKey:@"brf"];
  159. NSString *wyTxt = [jsonUv objectForKey:@"txt"];
  160. }
  161. -(void)requestForSuggestion:(NSDictionary*)jsonSuggestion{
  162. NSDictionary *wySuggestion = [jsonSuggestion objectForKey:@"suggestion"];
  163. NSDictionary *wyComf = [wySuggestion objectForKey:@"comf"];
  164. [self requestForComf:wyComf];
  165. NSDictionary *wyCw = [wySuggestion objectForKey:@"cw"];
  166. [self requestForCw:wyCw];
  167. NSDictionary *wyDrsg = [wySuggestion objectForKey:@"drsg"];
  168. [self requestForDrsg:wyDrsg];
  169. NSDictionary *wyFlu = [wySuggestion objectForKey:@"flu"];
  170. [self requestForFlu:wyFlu];
  171. NSDictionary *wySport = [wySuggestion objectForKey:@"sport"];
  172. [self requestForSport:wySport];
  173. NSDictionary *wyTrav = [wySuggestion objectForKey:@"trav"];
  174. [self requestForTrav:wyTrav];
  175. NSDictionary *wyUv = [wySuggestion objectForKey:@"uv"];
  176. [self requestForUv:wyUv];
  177. }
  178. -(void)request: (NSString*)httpUrl {
  179. NSError *error;
  180. //加载一个NSURL对象
  181. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:httpUrl]];
  182. //将请求的url数据放到NSData对象中
  183. NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  184. //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
  185. NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
  186. NSArray *jsonArr = [jsonDict objectForKey:@"HeWeather data service 3.0"];
  187. NSDictionary *jsonDictionary = [jsonArr objectAtIndex:];
  188. [self requestForApi:jsonDictionary];
  189. [self requestForBasic:jsonDictionary];
  190. [self requestForDailyForecast:jsonDictionary];
  191. [self requestForHourlyForecast:jsonDictionary];
  192. [self requestForNow:jsonDictionary];
  193. NSString *wyStatus = [[jsonDictionary objectForKey:@"aqi"] objectForKey:@"ok"];
  194. [self requestForSuggestion:jsonDictionary];
  195. }
  196. - (void)viewDidLoad {
  197. [super viewDidLoad];
  198. NSString *httpUrl = @"https://api.heweather.com/x3/weather?cityid=CN101210906&key=1cce17d4649344949ef7a619efec1cd5";
  199. [self request: httpUrl ];
  200. }

  

解析和风天气API的json(objective-C实现)的更多相关文章

  1. Android解析中国天气网的Json数据

    在Android开发中.一般的APP都是通过获取server端的数据来更新UI.从server获取到的数据能够是Json.它的数据量要比XML要小,这里解析中国天气网上获取的数据,尽管已经不再更新了. ...

  2. 常见天气api

    1. 心知天气API1.1 免费版:400次/小时,也就是9600次/天.国内城市数据,天气实况,3天预报,6项生活指数.这个API的免费版已经提供了很多年了,应该算最长寿稳定的那批API了……1.2 ...

  3. [转]真正的中国天气api接口xml,json

    转自:http://blog.csdn.net/fancylovejava/article/details/26102635 我只想说现在网上那几个api完全坑爹有木有??? 官方的申请不来有木有,还 ...

  4. (转)真正的中国天气api接口xml,json(求加精) ...

      我只想说现在网上那几个api完全坑爹有木有??? 官方的申请不来有木有,还有收费有木有?? 咱这种菜鸟只能用免费的了!!!! http://m.weather.com.cn/data/101110 ...

  5. 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  ...

  6. 中国天气api接口xml,json

    http://m.weather.com.cn/data/101110101.html 大坑有木有??反应慢不说了,还老不更新!! 想贴段代码的,现在又打不 开了(貌似3月4号以后没更新过) ==== ...

  7. JAVA的免费天气api接口调用示例

    step1:选择本文所示例的接口"免费天气api" url:https://www.juhe.cn/docs/api/id/39/aid/87 step2:每个接口都需要传入一个参 ...

  8. 雅虎天气API调用

    雅虎天气API调用: 1.调用方法:http://weather.yahooapis.com/forecastrss?w=2502265&u=c,绿色字体为城市代号,u=c表示取摄氏度. 2. ...

  9. 和风天气WebApi使用教程

    1.首先进入和风天气开发平台,点击右上角的注册进行注册 和风天气开发平台 2.填写注册用的邮箱和密码完成注册,可能还需要手机号,按提示注册完成即可. 3.从和风天气开发平台右上角进入控制台,输入你刚刚 ...

随机推荐

  1. C++的动态内存分配

  2. java 性能优化(代码优化)

    参考博文: java 性能优化:35 个小细节,让你提升 java 代码的运行效率

  3. SQL TOP 子句、SQL LIKE 操作符、SQL 通配符

    TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. SQL Server 的语法: S ...

  4. git学习手册

    #git学习手册 git: Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.[2] Git 是 Linus Torvalds 为了帮助管理 Linux内核开发而 ...

  5. 自动化测试selenium----css选择器总结

      选择器 示例 描述 * * 匹配任何元素 element DIV 标签选择器,匹配所有使用DIV标签的元素 .class .tips class选择器,匹配所有class属性中包含info的元素 ...

  6. logstash使用

    简介 Logstash是一个接收,处理,转发日志的工具.支持系统日志,webserver日志,错误日志,应用日志,总之包括所有可以抛出来的日志类型.怎么样听起来挺厉害的吧?在一个典型的使用场景下(EL ...

  7. C#中Object和Json之间的转换

    //创建一个类,此类有两个方法 using Newtonsoft.Json;namespace System{    public static class JsonExtension    {    ...

  8. 使用RestTemplate发送post请求

    最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...

  9. iOS JavaScriptCore与H5交互时出现异常提示

    在利用JavaScriptCore与H5交互时出现异常提示: This application is modifying the autolayout engine from a background ...

  10. Spring操作指南-IoC基础环境配置(基于XML)