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)
– megaultron
JSON contains a list of
weather
or has only one?– Jéf Bueno
Worked ?....
– novic
It worked, thank you very much for your answers!!
– Guilherme Barros