如何在C#中解析OpenWeather一个电话?
我已经成功地使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论