代码之家  ›  专栏  ›  技术社区  ›  Metzer

Javascript反序列化程序无法处理我的json对象-ASP。Net API应用程序

  •  0
  • Metzer  · 技术社区  · 7 年前

    我试过谷歌的各种解决方案,但都没有用。

    我从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
    }
    

    我做错了什么?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Der Kommissar    7 年前

    我做错了什么?

    实际上很多。从:你的 WeatherInfo 比赛 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 项都支持各种属性类型来实现这一点,如果您使用的是自制序列化程序,您可能有也可能没有,但看起来您使用的是内置的

        2
  •  0
  •   tech-y    7 年前

    您的代码似乎正确,您需要检查JSON对象。 This another post