首先申请api 获取AK码
然后获取JSON数据
然后解析JSON数据
最后呈现出读取的数据
个人修改 只呈现天气预报部分
注:其中使用的类库直接使用转载博文中类库
只是修改了其中的program.CS文件中的部分
修改部分如下
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);
}
}
}
}
1 WebClient类的DownloadString方法 使用 即把网页的数据返回以String形式返回
var json=WebClient.DownloadString(url)//返回JSON形式的数据
2 JSON数据的解析
本文学习转载的博文 使用的是
JsonConvert.DeserializeObject<BaiduTQ>(json);方法
要使用这个必须调用Newton.json 类库 以及Newton.json.dll文件
3 JSON格式解析的其他学习
3.1 http://www.cnblogs.com/QLJ1314/p/3862583.html
3.2 http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-json.html
3.3 http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html
4 转载的博客
4.1 http://www.daxueit.com/article/5477.html
4.2 备份网址http://blog.csdn.net/u010416101/article/details/44622087
5 转载工程示例的源代码
http://download.csdn.net/detail/u010416101/8532211
修改后的示例结果
原文:http://blog.csdn.net/u010416101/article/details/44622233