Javascript 反序列化程序不适用于我的 json 对象 - ASP.Net API 应用程序



我已经尝试了谷歌的各种解决方案,但无济于事。

我从 API 检索 JSON 对象。当我尝试将其反序列化为我的类对象时,它不起作用(并且在后续行的代码中得到"空对象引用"(:

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;
}

天气信息对象在反序列化 json 对象后保持 null。

关联的类:

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; }
}

杰森:

{
"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
}

我做错了什么?

我做错了什么?

实际上很多。从:您的WeatherInfo开始。这都是错误的 - JSON 序列化程序需要一个与 JSON 匹配的对象,或者具有指示为什么它JSON 不匹配的属性。您的对象两者都不执行。

为了工作它,我们需要对象匹配,以下 POCO 应该可以工作:

public class WeatherInfo
{
public Coordinate coord { get; set; }
public Weather[] weather { get; set; }
public string base { get; set; }
public MainInfo main { get; set; }
public float visibility { get; set; }
public WindInfo wind { get; set; }
public CloudInfo clouds { get; set; }
public long dt { get; set; }
public SysInfo sys { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
public class SysInfo
{
public int type { get; set; }
public int id { get; set; }
public float message { get; set; }
public string country { get; set; }
public long sunrise { get; set; }
public long sunset { get; set; }
}
public class CloudInfo
{
public float all { get; set; }
}
public class WindInfo
{
public float speed { get; set; }
public float deg { get; set; }
}
public class MainInfo
{
public float temp { get; set; }
public float pressure { get; set; }
public float humidity { get; set; }
public float temp_min { get; set; }
public float temp_max { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public struct Coordinate
{
public float lat { get; set; }
public float lon { get; set; }
}

完成此操作后,导航信息以收集所需内容应该是微不足道的。还应考虑查找序列化程序可能使用的属性,以便可以根据 .NET 命名约定命名这些属性,并使用该属性指定它们具有的 JSON 名称。JSON.NET项和JavaScriptSerializer项都支持各种属性类型来执行此操作,如果您使用的是自制序列化程序,则可能有也可能没有,但看起来您使用的是内置JavaScriptSerializer,因此我建议您查找它使用的属性并在您的工作中相应地更新这些模型。

你的代码看起来是正确的,你需要检查你的JSON对象。这篇关于堆栈溢出的另一篇文章应该对您有用。

最新更新