0

I have this snippet of code:

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

I want it to get description of it from forecast list.

But instead I'm getting this error

Object reference not set to an instance of an object

here's my whole list of classes:

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

anyone knows why does it appear?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
yuvi
  • 1
  • 3
  • The JSON most likely does not match the provided class definition which results in a null object when parsed – Nkosi Oct 12 '19 at 15:52
  • Have you tried to debug the code? What does download json contains? Where exactly do you get the error? – Sunil Oct 12 '19 at 15:55

2 Answers2

1

The JSON most likely does not match the provided class definition which results in a null object when parsed.

Calling the shown URL in a browser provides the following JSON response

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":"light intensity drizzle","icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":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,"name":"London","cod":200}

Which can be mapped to your shown code already provided with some modification.

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

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

There are online tools available where you can place the JSON and it will generate the mapped classes for the JSON.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

You can get weather in Xml using &mode=xml and then use xml serialization :

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; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20