如何在C#中解析OpenWeather一个电话?

发布于 2025-01-29 15:44:55 字数 2002 浏览 1 评论 0原文

我已经成功地使用JSON解析了OpenWeather的“当前天气数据”,但我对“一个电话”有疑问。它返回排列的数据有所不同。除了“天气”列表之外,我可以拉出所有内容。我为其创建的类返回零值。

{
"lat": 41.9585,
"lon": -78.1892,
"timezone": "America/New_York",
"timezone_offset": -14400,
"current": {
    "dt": 1652559634,
    "sunrise": 1652522008,
    "sunset": 1652574293,
    "temp": 74.53,
    "feels_like": 75.11,
    "pressure": 1014,
    "humidity": 73,
    "dew_point": 65.32,
    "uvi": 2.37,
    "clouds": 100,
    "visibility": 10000,
    "wind_speed": 7.47,
    "wind_deg": 173,
    "wind_gust": 10.63,
    "weather": [
        {
            "id": 804,
            "main": "Clouds",
            "description": "overcast clouds",
            "icon": "04d"
        }
    ]
}
}

string url = string.Format("https://api.openweathermap.org/data/2.5/onecall?lat={0}&lon={1}&exclude=minutely,hourly,daily&appid={2}&units=imperial", 41.958496166, -78.18916591, APIKey);

string jason = web.DownloadString(url);
WeatherInfo.root info = JsonConvert.DeserializeObject<WeatherInfo.root>(jason);


public static class WeatherInfo
{
    public class current
    {
        public double dt { get; set; }
        public double sunrise { get; set; }
        public double sunset { get; set; }
        public double temp { get; set; }
        public double pressure { get; set; }
        public double humidity { get; set; }
        public double dew_point { get; set; }
        public double uvi { get; set; }
        public double clouds { get; set; }
        public double visibility { get; set; }
        public double wind_speed { get; set; }
        public double wind_deg { get; set; }
        public double wind_gust { get; set; }
    }

    public class weather
    {
        public string main { get; set; }
        public string description { get; set; }
        public string icon { get; set; }
    }

    public class root
    {
        public current current { get; set; }
        public List<weather> weather { get; set; }
    }
}

I have successfully parsed Openweather's 'Current Weather Data' using Json, but I'm having a problem with 'One Call'. It returns the data arranged a little differently. I can pull out everything except the 'weather' list. The class I created for it returns null values.

{
"lat": 41.9585,
"lon": -78.1892,
"timezone": "America/New_York",
"timezone_offset": -14400,
"current": {
    "dt": 1652559634,
    "sunrise": 1652522008,
    "sunset": 1652574293,
    "temp": 74.53,
    "feels_like": 75.11,
    "pressure": 1014,
    "humidity": 73,
    "dew_point": 65.32,
    "uvi": 2.37,
    "clouds": 100,
    "visibility": 10000,
    "wind_speed": 7.47,
    "wind_deg": 173,
    "wind_gust": 10.63,
    "weather": [
        {
            "id": 804,
            "main": "Clouds",
            "description": "overcast clouds",
            "icon": "04d"
        }
    ]
}
}

string url = string.Format("https://api.openweathermap.org/data/2.5/onecall?lat={0}&lon={1}&exclude=minutely,hourly,daily&appid={2}&units=imperial", 41.958496166, -78.18916591, APIKey);

string jason = web.DownloadString(url);
WeatherInfo.root info = JsonConvert.DeserializeObject<WeatherInfo.root>(jason);


public static class WeatherInfo
{
    public class current
    {
        public double dt { get; set; }
        public double sunrise { get; set; }
        public double sunset { get; set; }
        public double temp { get; set; }
        public double pressure { get; set; }
        public double humidity { get; set; }
        public double dew_point { get; set; }
        public double uvi { get; set; }
        public double clouds { get; set; }
        public double visibility { get; set; }
        public double wind_speed { get; set; }
        public double wind_deg { get; set; }
        public double wind_gust { get; set; }
    }

    public class weather
    {
        public string main { get; set; }
        public string description { get; set; }
        public string icon { get; set; }
    }

    public class root
    {
        public current current { get; set; }
        public List<weather> weather { get; set; }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文