【发布时间】:2019-10-12 15:50:19
【问题描述】:
我有这段代码:
public void getForcast()
{
string url = string.Format("https://samples.openweathermap.org/data/2.5/weather?q=London&appid=b6907d289e10d714a6e88b30761fae22");
using (WebClient web = new WebClient())
{
var json = web.DownloadString(url);
var obj = JsonConvert.DeserializeObject<WeatherData.WeatherForcast>(json);
WeatherData.WeatherForcast forcast = obj;
WeatherMark.Text = string.Format("{0}", forcast.list[1].weathers[0].description);
}
}
我希望它从预测列表中获取它的描述。
但是我得到了这个错误
对象引用未设置为对象的实例
这是我的全部课程列表:
class WeatherForcast
{
public List<list> list { get; set; }
}
public class weather
{
public string main { get; set; }
public string description { get; set; }
}
public class list
{
public List<weather> weathers { get; set; }
}
有人知道它为什么会出现吗?
【问题讨论】:
-
JSON 很可能与提供的类定义不匹配,导致解析时为空对象
-
你试过调试代码吗?下载json包含什么?你究竟是从哪里得到错误的?