这是国家气象局提供的天气预报接口 [免费],似乎也没有次数限制,应该很适合个人开发者来调用。
这里以重庆市为例
调用地址:http://t.weather.sojson.com/api/weather/city/101040100。
最后面的地址是城市编码,可以获取未来15天的天气数据。
返回成功状态(status)为:200 ,失败为非200
如图:
返回的json格式如下:
{ "message": "success感谢又拍云(upyun.com)提供CDN赞助", "status": 200, "date": "20200619", "time": "2020-06-19 09:44:43", "cityInfo": { "city": "重庆市", "citykey": "101040100", "parent": "重庆", "updateTime": "09:31" }, "data": { "shidu": "65%", "pm25": 23, "pm10": 45, "quality": "优", "wendu": "29", "ganmao": "各类人群可自由活动", "forecast": [ { "date": "19", "high": "高温 30℃", "low": "低温 23℃", "ymd": "2020-06-19", "week": "星期五", "sunrise": "05:54", "sunset": "19:56", "aqi": 28, "fx": "东风", "fl": "2级", "type": "多云", "notice": "阴晴之间,谨防紫外线侵扰" }, { "date": "20", "high": "高温 28℃", "low": "低温 24℃", "ymd": "2020-06-20", "week": "星期六", "sunrise": "05:54", "sunset": "19:57", "aqi": 35, "fx": "东北风", "fl": "2级", "type": "小雨", "notice": "雨虽小,注意保暖别感冒" }, { "date": "21", "high": "高温 29℃", "low": "低温 23℃", "ymd": "2020-06-21", "week": "星期日", "sunrise": "05:54", "sunset": "19:57", "aqi": 36, "fx": "西北风", "fl": "1级", "type": "小雨", "notice": "雨虽小,注意保暖别感冒" }, { "date": "22", "high": "高温 25℃", "low": "低温 22℃", "ymd": "2020-06-22", "week": "星期一", "sunrise": "05:55", "sunset": "19:57", "aqi": 39, "fx": "西风", "fl": "1级", "type": "小雨", "notice": "雨虽小,注意保暖别感冒" }, { "date": "23", "high": "高温 27℃", "low": "低温 21℃", "ymd": "2020-06-23", "week": "星期二", "sunrise": "05:55", "sunset": "19:57", "aqi": 31, "fx": "北风", "fl": "2级", "type": "中雨", "notice": "记得随身携带雨伞哦" }, { "date": "24", "high": "高温 30℃", "low": "低温 20℃", "ymd": "2020-06-24", "week": "星期三", "sunrise": "05:55", "sunset": "19:57", "aqi": 28, "fx": "西南风", "fl": "1级", "type": "多云", "notice": "阴晴之间,谨防紫外线侵扰" }, { "date": "25", "high": "高温 32℃", "low": "低温 22℃", "ymd": "2020-06-25", "week": "星期四", "sunrise": "05:55", "sunset": "19:58", "aqi": 36, "fx": "东风", "fl": "1级", "type": "晴", "notice": "愿你拥有比阳光明媚的心情" }, { "date": "26", "high": "高温 34℃", "low": "低温 22℃", "ymd": "2020-06-26", "week": "星期五", "sunrise": "05:56", "sunset": "19:58", "aqi": 32, "fx": "东南风", "fl": "2级", "type": "阴", "notice": "不要被阴云遮挡住好心情" }, { "date": "27", "high": "高温 30℃", "low": "低温 22℃", "ymd": "2020-06-27", "week": "星期六", "sunrise": "05:56", "sunset": "19:58", "aqi": 24, "fx": "东北风", "fl": "2级", "type": "大雨", "notice": "出门最好穿雨衣,勿挡视线" }, { "date": "28", "high": "高温 32℃", "low": "低温 21℃", "ymd": "2020-06-28", "week": "星期日", "sunrise": "05:56", "sunset": "19:58", "aqi": 17, "fx": "西风", "fl": "1级", "type": "晴", "notice": "愿你拥有比阳光明媚的心情" }, { "date": "29", "high": "高温 32℃", "low": "低温 20℃", "ymd": "2020-06-29", "week": "星期一", "sunrise": "05:57", "sunset": "19:58", "aqi": 35, "fx": "东风", "fl": "1级", "type": "晴", "notice": "愿你拥有比阳光明媚的心情" }, { "date": "30", "high": "高温 33℃", "low": "低温 21℃", "ymd": "2020-06-30", "week": "星期二", "sunrise": "05:57", "sunset": "19:58", "aqi": 38, "fx": "东南风", "fl": "1级", "type": "多云", "notice": "阴晴之间,谨防紫外线侵扰" }, { "date": "01", "high": "高温 29℃", "low": "低温 20℃", "ymd": "2020-07-01", "week": "星期三", "sunrise": "05:57", "sunset": "19:58", "aqi": 32, "fx": "东南风", "fl": "2级", "type": "小雨", "notice": "雨虽小,注意保暖别感冒" }, { "date": "02", "high": "高温 24℃", "low": "低温 20℃", "ymd": "2020-07-02", "week": "星期四", "sunrise": "05:58", "sunset": "19:58", "aqi": 27, "fx": "南风", "fl": "2级", "type": "小雨", "notice": "雨虽小,注意保暖别感冒" }, { "date": "03", "high": "高温 29℃", "low": "低温 20℃", "ymd": "2020-07-03", "week": "星期五", "sunrise": "05:58", "sunset": "19:58", "aqi": 44, "fx": "东风", "fl": "2级", "type": "中雨", "notice": "记得随身携带雨伞哦" } ], "yesterday": { "date": "18", "high": "高温 27℃", "low": "低温 21℃", "ymd": "2020-06-18", "week": "星期四", "sunrise": "05:54", "sunset": "19:56", "aqi": 43, "fx": "西南风", "fl": "1级", "type": "阴", "notice": "不要被阴云遮挡住好心情" } } }
然后根据json的字段创建一个对象,使用NewtonSoft.Json反序化对象。
具体的城市代码可以在这个网址查看:http://www.wendangku.net/doc/461567915a8102d277a22f10.html
下面我已经整理出了一份txt文档的,大家可以通过写c#代码先格式化一下。
2020/6/19 11:04:59回复