C#访问web api获取json数据并对其进行解析
C#访问web api并获取json数据
1. 引用命名空间
using System.Net; //访问网络
using System.IO; //接收流数据
2. 创建WebRequest对象,使用静态方法Create(url)
string url = "https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=84567e9a80aaa95b24478261b2f92678";
WebRequest request = WebRequest.Create(url);
3. 创建WebResponse对象接受响应
WebResponse response = request.GetResponse();
4. 创建流对象读取数据并转换成字符串
Stream webstream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(webstream);
string json = streamReader.ReadToEnd();
5. 输出看看
对json数据进行解析
1. 添加引用
2. 引用命名空间
using System.Web.Script.Serialization;
3. 构造对应json格式的类(可以用网站http://json2csharp.chahuo.com/)
//存储天气json格式的类
public class Lives
{
public string province { get; set; }
public string city { get; set; }
public string adcode { get; set; }
public string weather { get; set; }
public string temperature { get; set; }
public string winddirection { get; set; }
public string windpower { get; set; }
public string humidity { get; set; }
public string reporttime { get; set; }
}
public class RootObject
{
public string status { get; set; }
public string count { get; set; }
public string info { get; set; }
public string infocode { get; set; }
public List<Lives> lives { get; set; }
}
4. 反序列化,将指定的 JSON 字符串转换为 RootObject类型的对象。
JavaScriptSerializer js = new JavaScriptSerializer();//实例化一个能够序列化数据的类
RootObject rootObject = js.Deserialize<RootObject>(json); //反序列化
string province = rootObject.lives[0].province;
string city = rootObject.lives[0].city;
string weather = rootObject.lives[0].weather;
string msg = String.Format("{0}-{1}的天气是:{2}", province, city, weather);
5. 输出看看
全部代码
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
//存储天气json格式的类
public class Lives
{
public string province { get; set; }
public string city { get; set; }
public string adcode { get; set; }
public string weather { get; set; }
public string temperature { get; set; }
public string winddirection { get; set; }
public string windpower { get; set; }
public string humidity { get; set; }
public string reporttime { get; set; }
}
public class RootObject
{
public string status { get; set; }
public string count { get; set; }
public string info { get; set; }
public string infocode { get; set; }
public List<Lives> lives { get; set; }
}
private void 天气ToolStripMenuItem_Click(object sender, EventArgs e)
{
string url = "https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=84567e9a80aaa95b24478261b2f92678";
WebRequest request = WebRequest.Create(url); //创建连接
WebResponse response = request.GetResponse();
Stream webstream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(webstream);
string json = streamReader.ReadToEnd(); //获取json数据
JavaScriptSerializer js = new JavaScriptSerializer();//实例化一个能够序列化数据的类
RootObject rootObject = js.Deserialize<RootObject>(json); //反序列化
string province = rootObject.lives[0].province;
string city = rootObject.lives[0].city;
string weather = rootObject.lives[0].weather;
string msg = String.Format("{0}-{1}的天气是:{2}", province, city, weather);
MessageBox.Show(msg);
}