从天气 API 返回指定值 - ASP.NET/C#

我一直在寻找一些时间来解决这个问题。我正在尝试将 Weather Unlocked 本地天气 API ( https://developer.weatherunlocked.com/documentation/localweather ) 添加到我的网站。但是我只能返回所有当前值。


我希望能够取出特定的项目,例如温度、湿度或纬度/经度,而不会出现其他所有内容。


代码


    const string WEBSERVICE_URL = "http://api.weatherunlocked.com/api/current/51.50,-0.12?app_id=42fd0793&app_key=cd2365f533caad77dc2d874aabc1625b";

    try

    {

        var webRequest = WebRequest.Create(WEBSERVICE_URL);


        if (webRequest != null)

        {

            webRequest.Method = "GET";

            webRequest.ContentType = "application/json";

            webRequest.Headers["X-API-Key"] = "cd2365f533caad77dc2d874aabc1625b";


            //Get the response 

            WebResponse wr = webRequest.GetResponseAsync().Result;

            Stream receiveStream = wr.GetResponseStream();

            StreamReader reader = new StreamReader(receiveStream);

            string content = reader.ReadToEnd();

            lblContent.Text = content;

        }

    }

    catch (Exception ex)

    {

        lblContent.Text = ex.ToString();

    }

结果


{

  "lat":51.5,

  "lon":0.05,

  "alt_m":5.0,

  "alt_ft":16.4,

  "wx_desc":"Clear skies",

  "wx_code":0,

  "wx_icon":"Clear.gif",

  "temp_c":7.0,

  "temp_f":44.6,

  "feelslike_c":2.6,

  "feelslike_f":36.68,

  "humid_pct":70.0,

  "windspd_mph":19.26,

  "windspd_kmh":31.0,

  "windspd_kts":16.74,

  "windspd_ms":8.61,

  "winddir_deg":250.0,

  "winddir_compass":"WSW",

  "cloudtotal_pct":25.0,

  "vis_km":10.0,

  "vis_mi":6.21,

  "vis_desc":null,

  "slp_mb":1002.0,

  "slp_in":29.67,

  "dewpoint_c":1.93,

  "dewpoint_f":35.47

}

显然,这很混乱,并非一切都是必要的。


提前致谢!


编辑 我尝试反序列化,但出现以下错误:


System.NullReferenceException: Object reference not set to an instance of an object

当我在反序列化中编写 foreach 循环时,我得到了这个。我的猜测是它正在调用“天气”类型的对象(类似于给定示例中对象 facebookFriend 的类型为 Facebook),并且没有任何内容存储在天气对象中。也许我在 API 的调用中做错了什么,我应该将它存储在我的 Weather 类中?


慕少森
浏览 101回答 1
1回答

杨魅力

我在手机上输入了这个,但是是这样的:using System;using System.Net;using Newtonsoft.Json;public class Program{&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string json = new WebClient().DownloadString("** YOUR URL **");&nbsp; &nbsp; &nbsp; &nbsp; WeatherData weatherData = JsonConvert.DeserializeObject<WeatherData>(json);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Lat:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" + weatherData.Lat);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Lon:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" + weatherData.Lon);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Alt_M:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" + weatherData.Alt_M);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Alt_FT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " + weatherData.Alt_FT);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("WX_Desc:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" + weatherData.WX_Desc);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("WX_Code:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" + weatherData.WX_Code);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("WX_Icon:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" + weatherData.WX_Icon);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Temp_C:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " + weatherData.Temp_C);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Temp_F:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " + weatherData.Temp_F);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Feelslike_C:&nbsp; &nbsp; &nbsp;" + weatherData.Feelslike_C);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Feelslike_F:&nbsp; &nbsp; &nbsp;" + weatherData.Feelslike_F);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Humid_PCT:&nbsp; &nbsp; &nbsp; &nbsp;" + weatherData.Humid_PCT);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Windspd_MPH:&nbsp; &nbsp; &nbsp;" + weatherData.Windspd_MPH);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Windspd_KMH:&nbsp; &nbsp; &nbsp;" + weatherData.Windspd_KMH);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Windspd_KTS:&nbsp; &nbsp; &nbsp;" + weatherData.Windspd_KTS);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Windspd_MS:&nbsp; &nbsp; &nbsp; " + weatherData.Windspd_MS);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Winddir_DEG:&nbsp; &nbsp; &nbsp;" + weatherData.Winddir_DEG);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Winddir_Compass: " + weatherData.Winddir_Compass);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Cloudtotal_PCT:&nbsp; " + weatherData.Cloudtotal_PCT);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Vis_KM:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " + weatherData.Vis_KM);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Vis_MI:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " + weatherData.Vis_MI);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Vis_Desc:&nbsp; &nbsp; &nbsp; &nbsp; " + weatherData.Vis_Desc);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("\n");&nbsp; &nbsp; }}public class WeatherData{&nbsp; &nbsp; public string Lat { get; set; }&nbsp; &nbsp; public string Lon { get; set; }&nbsp; &nbsp; public string Alt_M { get; set; }&nbsp; &nbsp; public string Alt_FT { get; set; }&nbsp; &nbsp; public string WX_Desc { get; set; }&nbsp; &nbsp; public string WX_Code { get; set; }&nbsp; &nbsp; public string WX_Icon { get; set; }&nbsp; &nbsp; public string Temp_C { get; set; }&nbsp; &nbsp; public string Temp_F { get; set; }&nbsp; &nbsp; public string Feelslike_C { get; set; }&nbsp; &nbsp; public string Feelslike_F { get; set; }&nbsp; &nbsp; public string Humid_PCT { get; set; }&nbsp; &nbsp; public string Windspd_MPH { get; set; }&nbsp; &nbsp; public string Windspd_KMH { get; set; }&nbsp; &nbsp; public string Windspd_KTS { get; set; }&nbsp; &nbsp; public string Windspd_MS { get; set; }&nbsp; &nbsp; public string Winddir_DEG { get; set; }&nbsp; &nbsp; public string Winddir_Compass { get; set; }&nbsp; &nbsp; public string Cloudtotal_PCT { get; set; }&nbsp; &nbsp; public string Vis_KM { get; set; }&nbsp; &nbsp; public string Vis_MI { get; set; }&nbsp; &nbsp; public string Vis_Desc { get; set; }&nbsp; &nbsp; // OTHER PROPERTIES HERE ...}
打开App,查看更多内容
随时随地看视频慕课网APP