全国天气查询API:https://www.juhe.cn/docs/api/id/39
基于C#的全国天气查询API调用代码实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using Xfrog.Net; using System.Diagnostics; using System.Web; //---------------------------------- // 全国天气预报调用示例代码 - 聚合数据 // 在线接口文档:http://www.juhe.cn/docs/39 // 代码中JsonObject类下载地址:http://download.csdn.net/download/gcm3206021155665/7458439 //---------------------------------- namespace ConsoleAPI { class Program { static void Main(string[] args) { string appkey = "*******************"; //配置您申请的appkey //1.根据城市名/id查询天气 string url1 = "http://v.juhe.cn/weather/index"; var parameters1 = new Dictionary<string, string>(); parameters1.Add("cityname" , ""); //城市名或城市ID,如:"苏州",需要utf8 urlencode parameters1.Add("dtype" , ""); //返回数据格式:json或xml,默认json parameters1.Add("format" , ""); //未来6天预报(future)两种返回格式,1或2,默认1 parameters1.Add("key", appkey);//你申请的key string result1 = sendPost(url1, parameters1, "get"); JsonObject newObj1 = new JsonObject(result1); String errorCode1 = newObj1["error_code"].Value; if (errorCode1 == "0") { Debug.WriteLine("成功"); Debug.WriteLine(newObj1); } else { //Debug.WriteLine("失败"); Debug.WriteLine(newObj1["error_code"].Value+":"+newObj1["reason"].Value); } //2.天气种类及标识列表 string url2 = "http://v.juhe.cn/weather/uni"; var parameters2 = new Dictionary<string, string>(); parameters2.Add("key", appkey);//你申请的key parameters2.Add("dtype" , ""); //返回数据的格式,xml或json,默认json string result2 = sendPost(url2, parameters2, "get"); JsonObject newObj2 = new JsonObject(result2); String errorCode2