zoukankan      html  css  js  c++  java
  • C#访问web api获取json数据并对其进行解析

    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);
    
    }
    
  • 相关阅读:
    前端学习笔记之BOM和DOM
    JAVA学习笔记之图解JAVA参数传递
    Python学习笔记之函数参数传递 传值还是传引用
    Java学习笔记之对象的复制和克隆
    如何科学正确的使用搜索引擎
    JAVA学习笔记之JAVA 对象引用以及赋值
    前端学习笔记之Z-index详解
    Python面试题目之Python的复制和赋值浅析
    Python面试题目之(针对dict或者set数据类型)边遍历 边修改 报错dictionary changed size during iteration
    判断对象是否为数组/函数
  • 原文地址:https://www.cnblogs.com/urahyou/p/14968054.html
Copyright © 2011-2022 走看看