简体   繁体   中英

Consuming web API into Blazor

I'm trying to consume a web API into Blazor but am struggling to extract certain information from the API

using System.Text.Json;

namespace CA3.Song
{
    public class SongService : ISongService
    {
        private readonly HttpClient _httpClient;
        const string _baseUrl = "https://genius-song-lyrics1.p.rapidapi.com/";
        const string _songEndpoint = "songs/chart?time_period=day&chart_genre=all&per_page=10&page=1";
        const string _host = "genius-song-lyrics1.p.rapidapi.com";
        const string _key = "{key}";

        public SongService(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }

        public async Task<List<SongItem>> GetSong()
        {
            Configure();

            var response = await _httpClient.GetAsync(_songEndpoint);
            var response_outcome = response.EnsureSuccessStatusCode;

            using var stream = await response.Content.ReadAsStreamAsync();

            var dto = await JsonSerializer.DeserializeAsync<SongDto>(stream);
            return dto.response.chart_items.Select(n => new SongItem { Artist = n.item.primary_artist, Title = n.item.primary_title, ReleaseDate = n.primary_release_date }).ToList().T ;
        }

        private void Configure()
        {
            _httpClient.BaseAddress = new Uri(_baseUrl);
            _httpClient.DefaultRequestHeaders.Add("X-RapidAPI-Host", _host);
            _httpClient.DefaultRequestHeaders.Add("X-Rapid-Key", _key);
        }
    }
}

The issue is on the return statement using LINQ.

namespace CA3.Song
{
    public class SongDto
    {
        public Meta meta { get; set; }
        public Response response { get; set; }
    }

    public class Meta
    {
        public int status { get; set; }
    }

    public class Response
    {
        public Chart_Items[] chart_items { get; set; }
        public int next_page { get; set; }
    }

    public class Chart_Items
    {
        public string _type { get; set; }
        public string type { get; set; }
        public Item item { get; set; }
    }

    public class Item
    {
        public string _type { get; set; }
        public int annotation_count { get; set; }
        public string api_path { get; set; }
        public string artist_names { get; set; }
        public string full_title { get; set; }
        public string header_image_thumbnail_url { get; set; }
        public string header_image_url { get; set; }
        public int id { get; set; }
        public bool instrumental { get; set; }
        public int lyrics_owner_id { get; set; }
        public string lyrics_state { get; set; }
        public int lyrics_updated_at { get; set; }
        public string path { get; set; }
        public int pyongs_count { get; set; }
        public string relationships_index_url { get; set; }
        public Release_Date_Components release_date_components { get; set; }
        public string release_date_for_display { get; set; }
        public string song_art_image_thumbnail_url { get; set; }
        public string song_art_image_url { get; set; }
        public Stats stats { get; set; }
        public string title { get; set; }
        public string title_with_featured { get; set; }
        public int updated_by_human_at { get; set; }
        public string url { get; set; }
        public Featured_Artists[] featured_artists { get; set; }
        public Primary_Artist primary_artist { get; set; }
    }

    public class Release_Date_Components
    {
        public int year { get; set; }
        public int month { get; set; }
        public int day { get; set; }
    }

    public class Stats
    {
        public int unreviewed_annotations { get; set; }
        public int concurrents { get; set; }
        public bool hot { get; set; }
        public int pageviews { get; set; }
    }

    public class Primary_Artist
    {
        public string _type { get; set; }
        public string api_path { get; set; }
        public string header_image_url { get; set; }
        public int id { get; set; }
        public string image_url { get; set; }
        public string index_character { get; set; }
        public bool is_meme_verified { get; set; }
        public bool is_verified { get; set; }
        public string name { get; set; }
        public string slug { get; set; }
        public string url { get; set; }
        public int iq { get; set; }
    }

    public class Featured_Artists
    {
        public string _type { get; set; }
        public string api_path { get; set; }
        public string header_image_url { get; set; }
        public int id { get; set; }
        public string image_url { get; set; }
        public string index_character { get; set; }
        public bool is_meme_verified { get; set; }
        public bool is_verified { get; set; }
        public string name { get; set; }
        public string slug { get; set; }
        public string url { get; set; }
        public int iq { get; set; }
    }

}

Above is the JSON file and I'm trying to extract the artist name, song title and release date from Item but it isn't in a list so I'm trying to find a way to extract this info using LINQ. Any help would be greatly appreciated !

I think you misinterpreted the declaration of the JSON. They are stored in an array Chart_Items[] chart_items thus you may use linq.

There are many ways to do this, I took advantage of named tuples in modern C# paired with linq.

Response r = new Response();//some resonse from api
var results = r.chart_items.Select(i => (i.item.artist_names, i.item.title, i.item.release_date_components)).ToList();

foreach(var result in results)
{
    string artist_names = result.artist_names;
    string title = result.title;
    Release_Date_Components release_date_components = result.release_date_components;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM