Get forecast from weatherapi.com

Jassim Al Rahma 1,546 Reputation points
2022-05-08T17:23:25.157+00:00

Hi,

I have below code to consume the forecast weather from weatherapi.com:

async void GetForecast()  
{  
    var client = new HttpClient();  
  
    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);  
    var result = await response.Content.ReadAsStringAsync();  
  
    CurrentWeather.Root data = JsonSerializer.Deserialize<CurrentWeather.Root>(result);  
  
    ListViewForecast.ItemsSource = data;  
}  

and I am getting the correct data from the JSON as you can see in below image:

199970-screen-shot-2022-05-08-at-92036-pm.png

but the problem is I am not able to show the data in my XAML. I tried:

Text="{Binding forecast.forecastday.hour.temp_c}"  

Here is my complete Class:

   using System;  
   using System.Collections.Generic;  
     
   namespace MyWeatherApp  
   {  
       class ForecastWeather  
       {  
           // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);  
           public class Astro  
           {  
               public string sunrise { get; set; }  
               public string sunset { get; set; }  
               public string moonrise { get; set; }  
               public string moonset { get; set; }  
               public string moon_phase { get; set; }  
               public string moon_illumination { get; set; }  
           }  
     
           public class Condition  
           {  
               public string text { get; set; }  
               public string icon { get; set; }  
               public int code { get; set; }  
           }  
     
           public class Current  
           {  
               public int last_updated_epoch { get; set; }  
               public string last_updated { get; set; }  
               public double temp_c { get; set; }  
               public double temp_f { get; set; }  
               public int is_day { get; set; }  
               public Condition condition { get; set; }  
               public double wind_mph { get; set; }  
               public double wind_kph { get; set; }  
               public int wind_degree { get; set; }  
               public string wind_dir { get; set; }  
               public double pressure_mb { get; set; }  
               public double pressure_in { get; set; }  
               public double precip_mm { get; set; }  
               public double precip_in { get; set; }  
               public int humidity { get; set; }  
               public int cloud { get; set; }  
               public double feelslike_c { get; set; }  
               public double feelslike_f { get; set; }  
               public double vis_km { get; set; }  
               public double vis_miles { get; set; }  
               public double uv { get; set; }  
               public double gust_mph { get; set; }  
               public double gust_kph { get; set; }  
           }  
     
           public class Day  
           {  
               public double maxtemp_c { get; set; }  
               public double maxtemp_f { get; set; }  
               public double mintemp_c { get; set; }  
               public double mintemp_f { get; set; }  
               public double avgtemp_c { get; set; }  
               public double avgtemp_f { get; set; }  
               public double maxwind_mph { get; set; }  
               public double maxwind_kph { get; set; }  
               public double totalprecip_mm { get; set; }  
               public double totalprecip_in { get; set; }  
               public double avgvis_km { get; set; }  
               public double avgvis_miles { get; set; }  
               public double avghumidity { get; set; }  
               public int daily_will_it_rain { get; set; }  
               public int daily_chance_of_rain { get; set; }  
               public int daily_will_it_snow { get; set; }  
               public int daily_chance_of_snow { get; set; }  
               public Condition condition { get; set; }  
               public double uv { get; set; }  
           }  
     
           public class Forecast  
           {  
               public List<Forecastday> forecastday { get; set; }  
           }  
     
           public class Forecastday  
           {  
               public string date { get; set; }  
               public int date_epoch { get; set; }  
               public Day day { get; set; }  
               public Astro astro { get; set; }  
               public List<Hour> hour { get; set; }  
           }  
     
           public class Hour  
           {  
               public int time_epoch { get; set; }  
               public string time { get; set; }  
               public double temp_c { get; set; }  
               public double temp_f { get; set; }  
               public int is_day { get; set; }  
               public Condition condition { get; set; }  
               public double wind_mph { get; set; }  
               public double wind_kph { get; set; }  
               public int wind_degree { get; set; }  
               public string wind_dir { get; set; }  
               public double pressure_mb { get; set; }  
               public double pressure_in { get; set; }  
               public double precip_mm { get; set; }  
               public double precip_in { get; set; }  
               public int humidity { get; set; }  
               public int cloud { get; set; }  
               public double feelslike_c { get; set; }  
               public double feelslike_f { get; set; }  
               public double windchill_c { get; set; }  
               public double windchill_f { get; set; }  
               public double heatindex_c { get; set; }  
               public double heatindex_f { get; set; }  
               public double dewpoint_c { get; set; }  
               public double dewpoint_f { get; set; }  
               public int will_it_rain { get; set; }  
               public int chance_of_rain { get; set; }  
               public int will_it_snow { get; set; }  
               public int chance_of_snow { get; set; }  
               public double vis_km { get; set; }  
               public double vis_miles { get; set; }  
               public double gust_mph { get; set; }  
               public double gust_kph { get; set; }  
               public double uv { get; set; }  
           }  
     
           public class Location  
           {  
               public string name { get; set; }  
               public string region { get; set; }  
               public string country { get; set; }  
               public double lat { get; set; }  
               public double lon { get; set; }  
               public string tz_id { get; set; }  
               public int localtime_epoch { get; set; }  
               public string localtime { get; set; }  
           }  
     
           public class Root  
           {  
               public Location location { get; set; }  
               public Current current { get; set; }  
               public Forecast forecast { get; set; }  
           }  
       }  
   }  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,602 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,836 Reputation points Microsoft Vendor
    2022-05-09T03:14:50.883+00:00

    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.

    1 person found this answer helpful.