C# 调用API接口

参考链接:https://blog.csdn.net/qq_35913274/article/details/79194046
get post 请求两种方式,我挑选了一种简单的方式

关于C#代码调用API接口获取到json数据的简单方法:

using (var client = new WebClient())
            {

                client.Encoding = Encoding.UTF8;
                string serviceAddress = "http://t.weather.sojson.com/api/weather/city/"+ 101270101;//请求URL地址
                var data = client.DownloadString(serviceAddress);
                var obj = JsonConvert.DeserializeObject<JObject>(data);
                Console.Write(obj);
                Console.ReadKey();

            }

如果只是获取到json数据并没多大用处 需要对其解析;

http://json2csharp.chahuo.com/ 转json格式为C#类;

http://tool.chinaz.com/tools/jsonescape.aspx json格式的各种操作;

https://www.sojson.com/blog/305.html 天气API

下面以获取天气API信息为例:

该API返回数据格式如下:

{
    "time": "2018-09-22 12:37:21",//系统更新时间
    "cityInfo": {
        "city": "天津市",  //请求城市
        "cityId": "101030100",//请求ID
        "parent": "天津",     //上级,一般是省份
        "updateTime": "12:32"//天气更新时间
    },
    "date": "20180922",     //当前天气的当天日期
    "message": "Success !", //返回message
    "status": 200,          //返回状态
    "data": {
        "shidu": "22%",     //湿度
        "pm25": 15.0,       //pm2.5
        "pm10": 46.0,       //pm10
        "quality": "优",    //空气质量
        "wendu": "24",      //温度
        "ganmao": "各类人群可自由活动",//感冒提醒(指数)
        "forecast": [//今天+未来14天
            {
                "date": "22",
                "ymd": "2018-09-22",   //年月日  (新增)
                "week": "星期六",       //星期 (新增)
                "sunrise": "05:57",
                "high": "高温 26.0℃",
                "low": "低温 15.0℃",
                "sunset": "18:10",
                "aqi": 55.0,
                "fx": "西北风",
                "fl": "4-5级",
                "type": "晴",
                "notice": "愿你拥有比阳光明媚的心情"
            },
            {
                "date": "23",
                "ymd": "2018-09-22",   //年月日  (新增)
                "week": "星期日",       //星期 (新增)
                "sunrise": "05:58",
                "high": "高温 23.0℃",
                "low": "低温 14.0℃",
                "sunset": "18:09",
                "aqi": 29.0,
                "fx": "西北风",
                "fl": "4-5级",
                "type": "晴",
                "notice": "愿你拥有比阳光明媚的心情"
            },
            {
                "date": "24",
                "ymd": "2018-09-22",   //年月日  (新增)
                "week": "星期一",       //星期 (新增)
                "sunrise": "05:59",
                "high": "高温 24.0℃",
                "low": "低温 15.0℃",
                "sunset": "18:07",
                "aqi": 25.0,
                "fx": "西北风",
                "fl": "<3级",
                "type": "晴",
                "notice": "愿你拥有比阳光明媚的心情"
            },
            {
                "date": "25",
                "ymd": "2018-09-22",   //年月日  (新增)
                "week": "星期二",       //星期 (新增)
                "sunrise": "06:00",
                "high": "高温 24.0℃",
                "low": "低温 16.0℃",
                "sunset": "18:05",
                "aqi": 56.0,
                "fx": "西南风",
                "fl": "<3级",
                "type": "晴",
                "notice": "愿你拥有比阳光明媚的心情"
            },
            {
                "date": "26",  
                "ymd": "2018-09-22",   //年月日  (新增)
                "week": "星期三",       //星期 (新增)
                "sunrise": "06:01",
                "high": "高温 24.0℃",
                "low": "低温 17.0℃",
                "sunset": "18:04",
                "aqi": 86.0,
                "fx": "西南风",
                "fl": "3-4级",
                "type": "阴",
                "notice": "不要被阴云遮挡住好心情"
            }
        ]
    }
}

生成接受返回数据的类

public class CityInfo
    {
        public string city { get; set; }
        public string citykey { get; set; }
        public string parent { get; set; }
        public string updateTime { get; set; }
    }

    public class Forecast
    {
        public string date { get; set; }
        public string high { get; set; }
        public string low { get; set; }
        public string ymd { get; set; }
        public string week { get; set; }
        public string sunrise { get; set; }
        public string sunset { get; set; }
        public string aqi { get; set; }
        public string fx { get; set; }
        public string fl { get; set; }
        public string type { get; set; }
        public string notice { get; set; }
    }

    public class Yesterday
    {
        public string date { get; set; }
        public string high { get; set; }
        public string low { get; set; }
        public string ymd { get; set; }
        public string week { get; set; }
        public string sunrise { get; set; }
        public string sunset { get; set; }
        public string aqi { get; set; }
        public string fx { get; set; }
        public string fl { get; set; }
        public string type { get; set; }
        public string notice { get; set; }
    }

    public class Data
    {
        public string shidu { get; set; }
        public string pm25 { get; set; }
        public string pm10 { get; set; }
        public string quality { get; set; }
        public string wendu { get; set; }
        public string ganmao { get; set; }
        public List<Forecast> forecast { get; set; }
        public Yesterday yesterday { get; set; }
    }

    public class RootObject
    {
        public string message { get; set; }
        public string status { get; set; }
        public string date { get; set; }
        public string time { get; set; }
        public CityInfo cityInfo { get; set; }
        public Data data { get; set; }
    }

然后调用接口获取指定格式的数据

using (var client = new WebClient())
            {

                client.Encoding = Encoding.UTF8;
                string serviceAddress = "http://t.weather.sojson.com/api/weather/city/"+ 101270101;
                var data = client.DownloadString(serviceAddress);
                RootObject datas = JsonConvert.DeserializeObject<RootObject>(data);
                Console.Write(datas.data.forecast[0].date);
                Console.ReadKey();

            }

然后就可以获取json里面的各种值;

Console.Write("更新时间是:"+datas.time);
                Console.Write("现在所处位置是:"+datas.cityInfo.city);
                foreach (var re in datas.data.forecast)
                {
                    Console.WriteLine("现在是" + re.ymd + "  " + re.week);
                    Console.WriteLine("最高温度" + re.high + "最低温度" + re.low);
                }

读取json文件获取里面的城市code:

public static string Readjson(string city_name)
        {

            //读取自定目录下的json文件
            StreamReader sr = new StreamReader(@"F:*****.json");
            string json = sr.ReadToEnd();
            //json文件转为 对象  T 创建的类 字段名 应该和json文件中的保持一致     
            var data = JsonConvert.DeserializeObject<RootObject>(json);
            foreach(var re in data.re)
            {
                if(re.city_name.Contains(city_name))
                {
                    return re.city_code;
                }
            }
            return null;
        }

城市文件的json类

public class Re
    {
        public string id { get; set; }
        public string pid { get; set; }
        public string city_code { get; set; }
        public string city_name { get; set; }
        public string post_code { get; set; }
        public string area_code { get; set; }
        public string ctime { get; set; }
    }

    public class RootObject
    {
        public List<Re> re { get; set; }
    }

城市json文件:

链接:https://pan.baidu.com/s/1oWnQVRebpGNN43Bde_gDHA
提取码:5slx

又知道了新方法
其实可以不用生成类 直接用dynamic强类型转化 超方便
在 NuGet里面引用jil,而且jil效率还贼快 当然还是转化成类后面使用方便些.

using (var client = new WebClient())
            {

                client.Encoding = Encoding.UTF8;
                string serviceAddress = "http://t.weather.sojson.com/api/weather/city/" + 101270101;
                var data = client.DownloadString(serviceAddress);
                //RootObject datas = JsonConvert.DeserializeObject(data);
                var result = JSON.Deserialize<dynamic>(data);
                Console.Write("更新时间是:" + result.time);
                Console.Write("现在所处位置是:" + result.cityInfo.city);
            }

你可能感兴趣的:(C#使用)