'Fetching data from an API in ASP.NET Core MVC

I'm facing problem while fetching data from an API in ASP.NET Core MVC controller (imdb api).

The problem is I cannot deserialize the current JSON object into type this is my code

List<Movie> movieList = new List<Movie>();

using (var httpClient = new HttpClient())
{
    using (var response = await httpClient.GetAsync("https://imdb-api.com/en/API/Title/****/tt0110413"))
    {
        string apiResponse = await response.Content.ReadAsStringAsync();
                
        var Movie = JsonConvert.DeserializeObject<Movie>(apiResponse);
        //movieList = JsonConvert.DeserializeObject<List<Movie>>(apiResponse);
    }
}

return View();

My api data looks like this

{
    "items": [
        {
            "id": "tt0111161",
            "rank": "1",
        },...
    ]
}

I have an issue with deserialize - any help?



Solution 1:[1]

You need to make sure the structure of Movie is corresponding to your json data:

public class Movie {
        public List<Item> items { get; set; }
    }
    public class Item {
        public string id { get; set; }
        public string rank { get; set; }

    }

Solution 2:[2]

Use IMDbApiLib.

GitHub: https://github.com/IMDb-API/IMDbApiLib

Nuget: https://nuget.org/packages/IMDbApiLib

Movie's TitleData: https://github.com/IMDb-API/IMDbApiLib/blob/master/IMDbApiLib/Models/TitleData.cs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yiyi You
Solution 2 Haddad