【问题标题】:Parsing Open Weather Map API response fails [duplicate]解析开放天气地图 API 响应失败 [重复]
【发布时间】: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包含什么?你究竟是从哪里得到错误的?

标签: c# .net xml wpf object


【解决方案1】:

JSON 很可能与提供的类定义不匹配,从而在解析时产生空对象。

在浏览器中调用显示的 URL 会提供以下 JSON 响应

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"毛毛雨","description":"light 强度 毛毛雨","icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"湿度":81,"temp_min":279.15, "temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600," sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"名称":"伦敦","鳕鱼":200}

可以映射到您已经提供的显示代码,并进行了一些修改。

public class WeatherForcast {
    public List<weather> weather { get; set; }
}

public class weather {
    public string main { get; set; }
    public string description { get; set; }
}

有一些在线工具可以用来放置 JSON,它会为 JSON 生成映射的类。

【讨论】:

    【解决方案2】:

    您可以使用 &mode=xml 获取 XML 中的天气,然后使用 xml 序列化:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    namespace ConsoleApplication1
    {
        class Program
        {
            const string URL = "https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22";
            static void Main(string[] args)
            {
                XmlReader reader = XmlReader.Create(URL);
    
                XmlSerializer serializer = new XmlSerializer(typeof(Weather));
                Weather weather = (Weather)serializer.Deserialize(reader);
            }
        }
        [XmlRoot("current")]
        public class Weather
        {
            public City city { get; set; }
            public Temperature temperature { get; set; }
            public Humidity humidity { get; set; }
            public Pressure pressure { get; set; }
            public Wind wind { get; set; }
        }
        public class City
        {
            [XmlAttribute()]
            public string name { get; set; }
    
            [XmlAttribute()]
            public string id { get; set; }
    
            public Coord coord { get; set; }
            public string country { get; set; }
            public Sun sun { get; set; }
        }
        public class Sun
        {
            [XmlAttribute()]
            public DateTime rise { get; set; }
    
            [XmlAttribute()]
            public DateTime set { get; set; }        
        }
        public class Coord
        {
            [XmlAttribute()]
            public decimal lon { get; set; }
    
            [XmlAttribute()]
            public decimal lat { get; set; }
        }
        public class Temperature
        {
            [XmlAttribute()]
            public decimal value { get; set; }
    
            [XmlAttribute()]
            public decimal min { get; set; }
    
            [XmlAttribute()]
            public decimal max { get; set; }
        }
        public class Humidity
        {
            [XmlAttribute()]
            public decimal value { get; set; }
        }
        public class Pressure
        {
            [XmlAttribute()]
            public decimal value { get; set; }
        }
        public class Wind
        {
            public Speed speed { get; set; }
            public Direction direction { get; set; }
        }
        public class Speed
        {
            [XmlAttribute()]
            public decimal value { get; set; }
    
            [XmlAttribute()]
            public string name { get; set; }
        }
        public class Direction
        {
            [XmlAttribute()]
            public decimal value { get; set; }
    
            [XmlAttribute()]
            public string name { get; set; }
    
            [XmlAttribute()]
            public string code { get; set; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 2021-08-23
      • 2015-03-02
      相关资源
      最近更新 更多