Search code examples
c#asp.net.net-corerazor-pagesasp.net-core-5.0

Http GET requests with complex json value in .NET Core 5 web app


I'm trying to do a get request, here's my razor page class:

public class IndexModel : PageModel
{
    public IEnumerable<ApiAnime> ApiData { get; set; } 

    public async Task<IActionResult> OnGet()
    {
        var request = new HttpRequestMessage(HttpMethod.Get,
            "https://api.aniapi.com/v1/anime");
        var client = _clientFactory.CreateClient();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        var response = await client.SendAsync(request);

        if (response.IsSuccessStatusCode)
        {
            var responseStream = await response.Content.ReadAsStreamAsync();

            ApiData = await JsonSerializer.DeserializeAsync<IEnumerable<ApiAnime>>(responseStream);
        }

        else
        {
            ApiData = new List<ApiAnime>();
        }

        return Page();
    }
}

This razor page class seems to work with simpler or more straight forward json values. But with this api "https://api.aniapi.com/v1/anime" it seems to not work. Now here's my json class:

public class ApiAnime
{

    public class Rootobject
    {
        public int status_code { get; set; }
        public string message { get; set; }
        public Data data { get; set; }
        public string version { get; set; }
    }

    public class Data
    {
        public int current_page { get; set; }
        public int count { get; set; }
        public Document[] documents { get; set; }
        public int last_page { get; set; }
    }

    public class Document
    {
        public int anilist_id { get; set; }
        public int mal_id { get; set; }
        public int format { get; set; }
        public int status { get; set; }
        public Titles titles { get; set; }
        public Descriptions descriptions { get; set; }
        public DateTime start_date { get; set; }
        public DateTime end_date { get; set; }
        public int season_period { get; set; }
        public int season_year { get; set; }
        public int episodes_count { get; set; }
        public int episode_duration { get; set; }
        public string trailer_url { get; set; }
        public string cover_image { get; set; }
        public string cover_color { get; set; }
        public string banner_image { get; set; }
        public string[] genres { get; set; }
        public int score { get; set; }
        public int id { get; set; }
        public int prequel { get; set; }
        public int sequel { get; set; }
    }
}

The error I get is

The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[AnimDbNet.ApiModel.ApiAnime].
ApiData = await JsonSerializer.DeserializeAsync<IEnumerable<ApiAnime>>(responseStream);

I'm using .NET Core 5. Does anyone know how to fix this error?


Solution

  • The error is from trying to parse the return value of the API which is not a JSON array but a JSON Object, not an IEnumerable.

     ApiData = await JsonSerializer.DeserializeAsync<IEnumerable<ApiAnime>>(responseStream);
    

    The solution is to parse directly to an ApiAnime.Rootobject and then use the apiData.Data to access the data on your Page. Your code would look like this.

    public class IndexModel : PageModel
    {
        public ApiAnime.Rootobject ApiData { get; set; } 
    
    public async Task<IActionResult> OnGet()
    {
        var request = new HttpRequestMessage(HttpMethod.Get,
            "https://api.aniapi.com/v1/anime");
        var client = _clientFactory.CreateClient();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
    
        var response = await client.SendAsync(request);
    
        if (response.IsSuccessStatusCode)
        {
            var responseStream = await response.Content.ReadAsStreamAsync();
    
            ApiData = await JsonSerializer.DeserializeAsync<ApiAnime.Rootobject>(responseStream);
        }
    
        else
        {
            ApiData = new ApiAnime.Rootobject();
        }
    
        return Page();
     }
    }