我试过谷歌的各种解决方案,但都没有用。
我从API中检索JSON对象。当我尝试将其反序列化到我的类对象中时,它不起作用(并且我在代码中的前几行中得到了“Null对象引用”:
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
JavaScriptSerializer serializer = new JavaScriptSerializer();
WeatherInfo weatherinfo = serializer.Deserialize<WeatherInfo>(json);
lblCity_Country.Text = weatherinfo.city.name + "," + weatherinfo.city.country;
lblDescription.Text = weatherinfo.list[0].weather[0].description;
lblTempMin.Text = string.Format("{0}.c", Math.Round(weatherinfo.list[0].temp.min, 1));
lblTempMax.Text = string.Format("{0}.c", Math.Round(weatherinfo.list[0].temp.max, 1));
lblHumidity.Text = weatherinfo.list[0].humidity.ToString();
tblWeather.Visible = true;
}
public class WeatherInfo
{
public City city { get; set; }
public List<List> list { get; set; }
}
public class City
{
public string name { get; set; }
public string country { get; set; }
}
JSON:
{
"coord": {
"lon": -0.13,
"lat": 51.51
},
"weather": [{
"idââ": 501,
"main": "Raââin",
"description"ââ: "moderate rain",
"icon": "10d"
}],
"base": "stations",
"main": {
"ââtemp": 14.54,
"pressââure": 1015,
"humiditâây": 87,
"temp_min": ââ13,
"temp_max": 16
},
ââ"visibility": 10000ââ,
"wind": {
"speed"ââ: 2.6,
"deg": 340
},
"ââclouds": {
"all": 92ââ
},
"dt": 1502279400,
ââ"sys": {
"type": 1,
ââ"id": 5091,
"messagââe": 0.0123,
"countryââ": "GB",
"sunriseââ": 1502253448,
"sunseâât": 1502307198
},
"idââ": 2643743,
"name": ââ"London",
"cod": 2ââ00
}
我做错了什么?