第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從天氣 API 返回指定值 - ASP.NET/C#

從天氣 API 返回指定值 - ASP.NET/C#

C#
慕少森 2022-10-23 14:08:23
我一直在尋找一些時(shí)間來解決這個問題。我正在嘗試將 Weather Unlocked 本地天氣 API ( https://developer.weatherunlocked.com/documentation/localweather ) 添加到我的網(wǎng)站。但是我只能返回所有當(dāng)前值。我希望能夠取出特定的項(xiàng)目,例如溫度、濕度或緯度/經(jīng)度,而不會出現(xiàn)其他所有內(nèi)容。代碼    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();    }結(jié)果{  "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}顯然,這很混亂,并非一切都是必要的。提前致謝!編輯 我嘗試反序列化,但出現(xiàn)以下錯誤:System.NullReferenceException: Object reference not set to an instance of an object當(dāng)我在反序列化中編寫 foreach 循環(huán)時(shí),我得到了這個。我的猜測是它正在調(diào)用“天氣”類型的對象(類似于給定示例中對象 facebookFriend 的類型為 Facebook),并且沒有任何內(nèi)容存儲在天氣對象中。也許我在 API 的調(diào)用中做錯了什么,我應(yīng)該將它存儲在我的 Weather 類中?
查看完整描述

1 回答

?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個贊

我在手機(jī)上輸入了這個,但是是這樣的:


using System;

using System.Net;

using Newtonsoft.Json;


public class Program

{

    public static void Main()

    {

        string json = new WebClient().DownloadString("** YOUR URL **");


        WeatherData weatherData = JsonConvert.DeserializeObject<WeatherData>(json);


        Console.WriteLine("Lat:             " + weatherData.Lat);

        Console.WriteLine("Lon:             " + weatherData.Lon);

        Console.WriteLine("Alt_M:           " + weatherData.Alt_M);

        Console.WriteLine("Alt_FT:          " + weatherData.Alt_FT);

        Console.WriteLine("WX_Desc:         " + weatherData.WX_Desc);

        Console.WriteLine("WX_Code:         " + weatherData.WX_Code);

        Console.WriteLine("WX_Icon:         " + weatherData.WX_Icon);

        Console.WriteLine("Temp_C:          " + weatherData.Temp_C);

        Console.WriteLine("Temp_F:          " + weatherData.Temp_F);

        Console.WriteLine("Feelslike_C:     " + weatherData.Feelslike_C);

        Console.WriteLine("Feelslike_F:     " + weatherData.Feelslike_F);

        Console.WriteLine("Humid_PCT:       " + weatherData.Humid_PCT);

        Console.WriteLine("Windspd_MPH:     " + weatherData.Windspd_MPH);

        Console.WriteLine("Windspd_KMH:     " + weatherData.Windspd_KMH);

        Console.WriteLine("Windspd_KTS:     " + weatherData.Windspd_KTS);

        Console.WriteLine("Windspd_MS:      " + weatherData.Windspd_MS);

        Console.WriteLine("Winddir_DEG:     " + weatherData.Winddir_DEG);

        Console.WriteLine("Winddir_Compass: " + weatherData.Winddir_Compass);

        Console.WriteLine("Cloudtotal_PCT:  " + weatherData.Cloudtotal_PCT);

        Console.WriteLine("Vis_KM:          " + weatherData.Vis_KM);

        Console.WriteLine("Vis_MI:          " + weatherData.Vis_MI);

        Console.WriteLine("Vis_Desc:        " + weatherData.Vis_Desc);

        Console.WriteLine("\n");

    }

}


public class WeatherData

{

    public string Lat { get; set; }

    public string Lon { get; set; }

    public string Alt_M { get; set; }

    public string Alt_FT { get; set; }

    public string WX_Desc { get; set; }

    public string WX_Code { get; set; }

    public string WX_Icon { get; set; }

    public string Temp_C { get; set; }

    public string Temp_F { get; set; }

    public string Feelslike_C { get; set; }

    public string Feelslike_F { get; set; }

    public string Humid_PCT { get; set; }

    public string Windspd_MPH { get; set; }

    public string Windspd_KMH { get; set; }

    public string Windspd_KTS { get; set; }

    public string Windspd_MS { get; set; }

    public string Winddir_DEG { get; set; }

    public string Winddir_Compass { get; set; }

    public string Cloudtotal_PCT { get; set; }

    public string Vis_KM { get; set; }

    public string Vis_MI { get; set; }

    public string Vis_Desc { get; set; }

    // OTHER PROPERTIES HERE ...

}


查看完整回答
反對 回復(fù) 2022-10-23
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號