How to get YouTube Data in C#?

Jassim Al Rahma 1,541 Reputation points
2022-10-25T14:48:12.67+00:00

Hi,

I am trying to use the Search in YouTube Data API but It's not populating my data to the ListView although I am getting the correct result.

Here is my code:

var client = new HttpClient();  
  
var response = await client.GetAsync("https://www.googleapis.com/youtube/v3/search?key=xxxxxxxxxxxxxx&q=bahrain&type=video,playlist.&part=snippet&maxResults=50");  
  
if (response.IsSuccessStatusCode)  
{  
    var result = await response.Content.ReadAsStringAsync();  
  
    // YouTubeData.Root data = JsonSerializer.Deserialize<YouTubeData.Root>(result);  
  
    var data = JsonSerializer.Deserialize<List<YouTubeData.Root>>(result);  
  
    ObservableCollection<YouTubeData.Root> trends = new ObservableCollection<YouTubeData.Root>(data);  
  
    // List<Root> data = JsonSerializer.Deserialize<List<Root>>(result);  
  
    ListViewEarTube.ItemsSource = trends;  
}  
else  
{  
    await App.Current.MainPage.DisplayAlert("Error", "Error", "Error");  
}  

and this is my data Class:

namespace MyApp  
{  
    class YouTubeData  
    {  
        // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);  
        public class Default  
        {  
            public string url { get; set; }  
            public int width { get; set; }  
            public int height { get; set; }  
        }  
  
        public class High  
        {  
            public string url { get; set; }  
            public int width { get; set; }  
            public int height { get; set; }  
        }  
  
        public class Id  
        {  
            public string kind { get; set; }  
            public string videoId { get; set; }  
        }  
  
        public class Item  
        {  
            public string kind { get; set; }  
            public string etag { get; set; }  
            public Id id { get; set; }  
            public Snippet snippet { get; set; }  
        }  
  
        public class Medium  
        {  
            public string url { get; set; }  
            public int width { get; set; }  
            public int height { get; set; }  
        }  
  
        public class PageInfo  
        {  
            public int totalResults { get; set; }  
            public int resultsPerPage { get; set; }  
        }  
  
        public class Root  
        {  
            public string kind { get; set; }  
            public string etag { get; set; }  
            public string nextPageToken { get; set; }  
            public string regionCode { get; set; }  
            public PageInfo pageInfo { get; set; }  
            public List<Item> items { get; set; }  
        }  
  
        public class Snippet  
        {  
            public DateTime publishedAt { get; set; }  
            public string channelId { get; set; }  
            public string title { get; set; }  
            public string description { get; set; }  
            public Thumbnails thumbnails { get; set; }  
            public string channelTitle { get; set; }  
            public string liveBroadcastContent { get; set; }  
            public DateTime publishTime { get; set; }  
        }  
  
        public class Thumbnails  
        {  
            public Default @default { get; set; }  
            public Medium medium { get; set; }  
            public High high { get; set; }  
        }  
    }  
}  

Thanks
Jassim

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,481 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,406 Reputation points
    2022-10-25T18:44:37.38+00:00

    Based on the Youtube data API docs it looks like the response is a single object:
    https://developers.google.com/youtube/v3/docs/search/list#response

    But the response is being deserialised as a List<YouTubeData.Root> instead:

       var data = JsonSerializer.Deserialize<List<YouTubeData.Root>>(result);  
    

    I believe if you change this to YouTubeData.Root and then pass data.items into your ObservableCollection then you should be closer to fixing it.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful