C# 获取天气预报(未来15天)

参考:https://blog.csdn.net/zsj777/article/details/102456224

查看城市ID:http://www.wendangku.net/doc/461567915a8102d277a22f10-9.html

获取指定城市ID的未来15天的天气预报:http://t.weather.sojson.com/api/weather/city/101180205

示例代码:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

 city 城市ID:101091006
 public static ArrayList getWeather2(String city)
        {
            ArrayList al = new ArrayList();
            //1.根据城市查询天气
            //城市ID:101091006
            // city = 101091006;
            string url1 = "http://t.weather.sojson.com/api/weather/city/"+ city;

            var parameters1 = new Dictionary<string, string>();

            //parameters1.Add("cityname", city); //要查询的城市,如:温州、上海、北京
            //parameters1.Add("dtype", ""); //返回数据的格式,xml或json,默认json

            string result1 = sendPost(url1, parameters1, "get");

            //JsonObject newObj1 = new JsonObject(result1);
            //String errorCode1 = newObj1["error_code"].Value;
            JObject jo = (JObject)JsonConvert.DeserializeObject(result1);
            //JToken record = jo["error_code"];
            String errorCode1 = jo["status"].ToString();

            String info = "";

            if (errorCode1 == "200")
            {
                //"20200710"
                string date = jo["date"].ToString();
                string year = date.Substring(0, 4);
                string month = date.Substring(4, 2);

                //Debug.WriteLine("成功");
                //Debug.WriteLine(newObj1);
                JToken record = jo["data"];
                JToken record2 = record["forecast"];
                JToken[] jArray = record2.ToArray();

                for(int i=0;i< jArray.Length; i++)
                {
                    ArrayList temp_arr = new ArrayList();

                    JToken record3 = jArray[i];                    
                    string time = year + "-" + month + "-" + record3["date"];
                    temp_arr.Add(time);

                    string weather = record3["type"]+" "+ record3["low"].ToString().Trim().Replace("低温", "")+ "~"+ record3["high"].ToString().Trim().Replace("高温", "");
                    temp_arr.Add(weather);

                    al.Add(temp_arr);
                }
            }
            else
            {
                //Debug.WriteLine("失败");
                // Debug.WriteLine(newObj1["error_code"].Value + ":" + newObj1["reason"].Value);
            }

            return al;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值