根据返回的Json定义出相应的数据结构:
复制代码 public class BaiduTQ
{
public int error { get; set; }
public string status { get; set; }
public string date { get; set; }
public List<BaiduResult> results { get; set; }
}
public class BaiduResult
{
public string currentCity { get; set; }
public string pm25 { get; set; }
public List<BaiduIndex> index { get; set; }
public List<BaiDuWeaterData> weather_data { get; set; }
}
public class BaiduIndex
{
public string title { get; set; }
public string zs { get; set; }
public string tipt { get; set; }
public string des { get; set; }
}
public class BaiDuWeaterData
{
public string date { get; set; }
public string dayPictureUrl { get; set; }
public string nightPictureUrl { get; set; }
public string weather { get; set; }
public string wind { get; set; }
public string temperature { get; set; }
}
然后直接通过Newtonsoft.Json 反序列化成即可。
既然是获取天气,肯定是希望获取客户所在城市的天气,下一步则是需要根据用户机器IP获取所在城市,然后获取该城市的天气信息。
二、IP获取城市
通过淘宝的IP库,http://www.easck.com/,即可查询指定IP所在的城市、国家、运营商等。
有了上面的途径,我们下一步的工作就是获取客户的外网IP,而外网IP,是机器连接外网才会有,所以楼主写了一个页面,部署在外网服务器。










