参考: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;
}