Hello,
Firstly, we cannot use Text="{Binding forecast.forecastday.hour.temp_c}"
to get temp_c directly. Because forecastday
and hour
are List<>
.
I use free Api from weatherapi to get forecast. When we deserialize Json to get ForecastWeather.Root
object, we need to ForecastWeather.Forecast
object by data.forecast
, then get List<ForecastWeather.Forecastday>
by forecast.forecastday
. Now we get a List, I make a test to get the first record, So I use forecastdays[0]
to get List<Hour>
, it is a List as well, I get the first record for testing by forecastdays[0].hour[0]
, then we can get the temp_c
property like following code.
async void GetForecast()
{
var client = new HttpClient();
string url = "https://api.weatherapi.com/v1/forecast.json?key=8c1e055xxxxxxxxxxxxxxxxxx905&q=London&days=7&aqi=yes&alerts=no";
//string url = string.Format("{0}/current.json?key={1}&q={2},{3}", App.WeatherURL, App.WeatherAPI, App.Latitude, App.Longitude);
client.BaseAddress = new Uri(url);
var response = await client.GetAsync(url);
string result = await response.Content.ReadAsStringAsync();
ForecastWeather.Root data = JsonSerializer.Deserialize<ForecastWeather.Root>(result);
ForecastWeather.Forecast forecast = data.forecast;
List<ForecastWeather.Forecastday> forecastdays= forecast.forecastday;
double s=forecastdays[0].hour[0].temp_c;
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.