1

I am new to C#. I ma trying to create a console weather app. I have fetched JSON data from OpenWeather API which looks like this:

  "coord": {
    "lon": 27.5667,
    "lat": 53.9
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }
  ],

I have called JsonConvert.DeserializeObject<WeatherInfo>(stringResult);

I am able to deserialize the coord part, however the weather part is an array, how do I deserialize it?

private class WeatherInfo
        {
            public Coord Coord { get; set; }

            public Weather Weather { get; set; }
        }

        private class Weather
        {
            public readonly string Id;
            public readonly string Main;
            public readonly string Description;
            public readonly string Icon;

            public Weather(string lat, string lon, string id, string main, string description, string icon)
            {
                Id = id;
                Main = main;
                Description = description;
                Icon = icon;
            }
        }
        
        private class Coord
        {
            public readonly string Lat;
            public readonly string Lon;

            public Coord(string lat, string lon)
            {
                Lat = lat;
                Lon = lon;
            }
        }
        ```
11
  • 2
    public Weather[] Weather { get; set; }? Commented Jan 18, 2021 at 14:30
  • @gunr2171 I looked, but feel free. Commented Jan 18, 2021 at 14:33
  • 1
    Fildor got it right. If your JSON property is an array, you need to treat your c# property as a collection (array, list, IEnumerable, ICollection, etc). Commented Jan 18, 2021 at 14:34
  • 1
    @Fildor understand. I just had an account shut down because my questions weren't good enough. It can be very frustrating on a newbie who is already overwhelmed by the programming langauge, you know? Commented Jan 18, 2021 at 14:43
  • 1
    @Fildor Thanks, you are awesome. And thanks for clearing that all up man. Commented Jan 18, 2021 at 14:46

1 Answer 1

1

Use public Weather[] Weather { get; set; } to map.The C# Object Equivalent of your JSON will be like this

public partial class Temperatures
{
    [JsonProperty("coord")]
    public Coord Coord { get; set; }

    [JsonProperty("weather")]
    public Weather[] Weather { get; set; }
}

public partial class Coord
{
    [JsonProperty("lon")]
    public double Lon { get; set; }

    [JsonProperty("lat")]
    public double Lat { get; set; }
}

public partial class Weather
{
    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("main")]
    public string Main { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("icon")]
    public string Icon { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.