您的位置:首页 > 其它

从中国天气网上获取天气数据并解析

2010-02-05 16:19 393 查看
中国天气网http://www.weather.com.cn/上提供了很多有价值的天气数据,包含全国到县级地区的天气数据。可惜的是没有很好的数据共享接口, 该网站上提供了各种各样的插件 能满足一般网站的需求,但是对于只需要天气数据的而言还是不大方便~ 不过通过下面的方式可以获取指定地点的天气数据。 C#代码 public string GetWeatherData()
{
string path = " http://m.weather.com.cn/data/";
string[] citys = { "******", "******", "******", "******", "******" };// 城市编号 自己去找吧~
string html = "{\"result\":["; //返回的数据
foreach (string url in citys)
{
WebRequest req = WebRequest.Create(path + url + ".html");
WebResponse res = req.GetResponse();
Stream receiveStream = res.GetResponseStream();
Encoding encode = Encoding.GetEncoding("UTF-8");
StreamReader sr = new StreamReader(receiveStream, encode);
string temp = sr.ReadToEnd();
temp = temp.Substring(0, temp.Length - 1); temp += "},";
html += temp;
}
html = html.Substring(0, html.Length - 1);
html += "]}";
return html; }

这样最后得到的是一个json对象,我们在程序里进行解析就行了。

weatherinfo对象部分数据说明--------- index_uv:紫外线指数
index :穿衣指数
temp1 :温度
weather1:天气情况
fx1 :风向
fl1 :风力
date_y :日期
fchh :时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: