Json property help

Eduardo Gomez 4,136 Reputation points
2022-07-12T19:13:02.437+00:00

Update

Ok so this is the problem

I have my class with a bunch of properties like the country, flag, etc. I am using syncfution AutoComplete to display the city name and the flag

<syncfusion:SfTextBoxExt Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" ShowDropDownButton="True" SearchItemPath="city" AutoCompleteMode="Suggest" SelectedItem="{Binding SelectedItem}" HighlightedTextColor="Red" TextHighlightMode="FirstOccurrence" AutoCompleteSource="{Binding AirportsList}" >

            &lt;syncfusion:SfTextBoxExt.AutoCompleteItemTemplate&gt;
                &lt;DataTemplate&gt;
                    &lt;StackPanel Orientation=&#34;Horizontal&#34;&gt;
                        &lt;Image Width=&#34;12&#34; Source=&#34;{Binding flag}&#34; /&gt;
                        &lt;TextBlock Text=&#34;{Binding city}&#34; /&gt;
                    &lt;/StackPanel&gt;
                &lt;/DataTemplate&gt;
            &lt;/syncfusion:SfTextBoxExt.AutoCompleteItemTemplate&gt;
        &lt;/syncfusion:SfTextBoxExt&gt;

In order to get the flag, I need the countryCode, but my original JSON doesn't provide it, so I am doing another API call, passing the country name, to get the code in order to get the flag, but the thing is that this is not returning my list

code https://github.com/eduardoagr/Travel

Demo with voice explanation

https://upm365-my.sharepoint.com/:v:/g/personal/e_gromero_alumnos_upm_es/Ecz7amFxi-9FmnVMfRw3btYBmL_vv5yhFdkXtVIAUKtMgw?e=7bXXdg

Developer technologies | Windows Presentation Foundation
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,706 Reputation points Microsoft External Staff
    2022-07-18T03:51:07.163+00:00

    You could refer to the following code to add data to ISO2. Since the data is too large, I tested 10 data.

    Xaml:

    <Grid.RowDefinitions>  
                    <RowDefinition Height="Auto" />  
                    <RowDefinition Height="Auto" />  
                    <RowDefinition Height="Auto" />  
                    <RowDefinition Height="Auto" />  
                </Grid.RowDefinitions>  
     <DataGrid x:Name="dg" ItemsSource="{Binding AirportsList}"  Height="300" Grid.Column="1" Grid.Row="3"/>  
    

    AirportServices:

     public async Task<List<Airport>?> GetAirportsAsync(string url)  
        {  
            if (airports?.Count > 0)  
            {  
                return airports;  
            }  
            using (var client = new HttpClient())  
            {  
                var content = await client.GetStringAsync(url);  
                airports = JsonConvert.DeserializeObject<List<Airport>?>(content);  
                MessageBox.Show(airports.Count.ToString());  
                //for (int i = 0; i < airports.Count; i++)  
                for (int i =0;i < 10;i++)  
                {  
                    //var t = Task.Run(() => GetCountryCodeAsync($"https://countrycode.dev/api/countries"));  
                    //if(t!=null)  
                    //{  
                    //    var list = JsonConvert.DeserializeObject<List<string>?>(t.Result);  
                    //    airports[i].ISO2 = list[i].ToString();  
                    //}  
                    var response = string.Empty;  
                    using (var client1 = new HttpClient())  
                    {  
                        HttpResponseMessage result = await client1.GetAsync($"https://countrycode.dev/api/countries");  
                        if (result.IsSuccessStatusCode)  
                        {  
                            response = await result.Content.ReadAsStringAsync();  
                            var list = JsonConvert.DeserializeObject<List<string>>(response);  
                            airports[i].ISO2 = list[i].ToString();  
      
                        }  
                    }  
                }  
                return airports;  
            }  
            return null;  
             
        }  
        public async Task<string?> GetCountryCodeAsync(string url)  
        {  
            var response = string.Empty;  
            using (var client = new HttpClient())  
            {  
                HttpResponseMessage result = await client.GetAsync(url);  
                if (result.IsSuccessStatusCode)  
                {  
                    response = await result.Content.ReadAsStringAsync();  
                    return response;  
                }  
                return null;  
            }  
             
        }  
    

    The result:
    221737-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.