- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using Newtonsoft.Json;
- using System.Xml.Serialization;
- using System.IO;
- using System.Xml;
- namespace BDCode {
- class Program {
- static void Main(string[] args) {
- using(var client = new webClient()) {
- //获取所在城市
- client.Encoding = Encoding.UTF8;
- var url = "http://ip.taobao.com/service/getIpInfo.php?ip=27.37.26.219";
- var json = client.DownloadString(url);
- var ip = JsonConvert.DeserializeObject < TIPData > (json);
- ip.data.city = "南京";
- //获取天气
- url = "http://api.map.baidu.com/telematics/v3/weather?location=" + ip.data.city + "&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9";
- json = client.DownloadString(url);
- var tq = JsonConvert.DeserializeObject < BaiduTQ > (json);
- Console.WriteLine("{0} {1}", tq.results[0].currentCity, tq.results[0].weather_data[0].date);
- Console.WriteLine("error:{0} status:{1} date{2}", tq.error, tq.status, tq.date);
- Console.WriteLine("currentCity:{0} pm2.5:{1}", tq.results[0].currentCity, tq.results[0].pm25);
- for (int i = 0; i < tq.results[0].weather_data.Count; i++) {
- Console.WriteLine("");
- Console.WriteLine(" date:{0} \n dayPictureUrl:{1} \n nightPictureUrl:{2} \n waeather:{3} \n wind:{4} \n temperature:{5} \n ", tq.results[0].weather_data[i].date, tq.results[0].weather_data[i].dayPictureUrl, tq.results[0].weather_data[i].nightPictureUrl, tq.results[0].weather_data[i].weather, tq.results[0].weather_data[i].wind, tq.results[0].weather_data[i].temperature);
- }
- //Console.WriteLine("");
- /*
- //获取百度新闻数据
- url = "http://news.baidu.com/n?cmd=7&loc=0&name=%B1%B1%BE%A9&tn=rss";
- client.Encoding = Encoding.GetEncoding("gb2312");
- var xml = client.DownloadString(url);
- var rss = Deserialize<Rss>(xml);
- foreach (var news in rss.channel.item)
- {
- Console.WriteLine("标题:{0}", news.title);
- Console.WriteLine("链接:{0}", news.link);
- Console.WriteLine("来源:{0}", news.source);
- Console.WriteLine("描述:{0}", news.description);
- Console.WriteLine();
- }
- * */
- Console.ReadLine();
- }
- }
- /// <summary>
- /// 反序列化
- /// </summary>
- public static T Deserialize < T > (string xmlContent) {
- XmlSerializer xs = new XmlSerializer(typeof(T));
- using(StringReader strReader = new StringReader(xmlContent)) {
- XmlReader xmlReader = XmlReader.Create(strReader);
- return (T) xs.Deserialize(xmlReader);
- }
- }
- }
- }
来源: http://lib.csdn.net/snippet/dotnet/49144