How to return the values of a public List<Weather>?

Asked

Viewed 48 times

0

I have an app in Windows Weather/Weather Forms

I want to pull the Description data from the Weather Class

public class weather
{
    public string descrição { get; set; }
}

void getWeather(string city)
{
    using (WebClient web = new WebClient())
    {
        string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}&units=metric&cnt==6", city, APPID);

        var json = web.DownloadString(url);

        var result = JsonConvert.DeserializeObject<weatherInfo.root>(json);

        weatherInfo.root outPut = result;

        txtCidade.Text = string.Format("{0}", outPut.name);
        txtdescricao.Text = string.Format("{0}", outPut.sys.country);
        txtTemp.Text = string.Format("{0} \u00B0" + "C", outPut.main.temp);
        txtSensacao.Text = string.Format("{0} \u00B0" + "C", outPut.main.feels_like);
        txtHumidity.Text = string.Format("{0}" + "%", outPut.main.humidity);
        txtdescricao.Text = string.Format("{0}" + "%", outPut.weather.descricao);// O erro ocorre aqui, mas porque? 
    }
}

class weatherInfo
{
    public class coord
    {
        public double lon { get; set; }
        public double lat { get; set; }
    }

    public class weather
    {
        public int id { get; set; }
        public string descrição { get; set; }
        public string main { get; set; }
    }

    public class main
    {
        public double temp { get; set; }
        public double pressure { get; set; }
        public double humidity { get; set; }
        public double feels_like { get; set; }
        public double temp_min { get; set; }
    }

    public class wind
    {
        public double speed { get; set; }
    }

    public class sys
    {
        public string country { get; set; }
    }

    public class root
    {
        public string name { get; set; }
        public sys sys { get; set; }
        public double dt { get; set; }
        public wind wind { get; set; }
        public main main { get; set; }
        public List<weather> weather { get; set; }
        public coord coord { get; set; }
    }
}

Only he returns this mistake

CS1061 praList<weatherInfo.Weather>' does not contain a definition for "Description" and could not find any extension method "Description" accepting a first argument of the type List<weatherInfo.Weather>'

I’ve tried to:

txtdescricao.Text = string.Format("{0}" + "%", outPut.weather);
txtdescricao.Text = string.Format("{1}", outPut.weather);

How can I return the Results

public int id { get; set; }
public string descrição { get; set; }
public string main { get; set; }

For a Label?

  • You are trying to get a value directly from List, if you want to get the description the recommended is to join the strings, or get a specific description from the list. Example outPut.weather[0].descrição)

  • JSON contains a list of weather or has only one?

  • Worked ?....

  • It worked, thank you very much for your answers!!

1 answer

0

The feedback of this information is a in the following format:

{
  "coord": {
    "lon": -46.64,
    "lat": -23.55
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 25.56,
    "feels_like": 22.86,
    "temp_min": 25.56,
    "temp_max": 25.56,
    "pressure": 1012,
    "humidity": 44
  },
  "visibility": 10000,
  "wind": {
    "speed": 4.92,
    "deg": 327,
    "gust": 5.81
  },
  "clouds": {
    "all": 0
  },
  "dt": 1603889573,
  "sys": {
    "type": 3,
    "id": 2033898,
    "country": "BR",
    "sunrise": 1603873367,
    "sunset": 1603919858
  },
  "timezone": -10800,
  "id": 3448439,
  "name": "São Paulo",
  "cod": 200
}

where the weather is a array of a data type and at each position there is no such description, but in this case Description and to access need to pass to the code the position, because array may or may not possess items and each position within that array will represent a certain type.

In your code then:

txtdescricao.Text = string.Format("{0}" + "%", outPut.weather[0].description);

but, the correct is to know if this list has any item, so I would still do so:

txtdescricao.Text = "Não há resultado";
if (outPut.weather.Count() > 0) 
{
    txtdescricao.Text = string.Format("{0}" + "%", outPut.weather[0].description);
}

Stay tuned for these basic details and study more the language and how it works.

  • It worked, thank you very much!! I will study more about the API

Browser other questions tagged

You are not signed in. Login or sign up in order to post.