1

How is it possible to let users search for a movie using TMDB API (The Movie Db) in an ASP.NET MVC 5 application and return a JSON result.

Working example outside VB using my personel api key and returning a json result with all the movies containing the string "mission":

http://api.themoviedb.org/3/search/movie?api_key=841c..&query=mission

The documentation (http://docs.themoviedb.apiary.io/#reference/search/searchmovie) suggest to use the following code for C#:

var baseAddress = new Uri("http://api.themoviedb.org/3/");

using (var httpClient = new HttpClient{ BaseAddress = baseAddress })
{

    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");

    using(var response = await httpClient.GetAsync("search/movie"))
    {
        string responseData = await response.Content.ReadAsStringAsync();
    }
}

I paste the code into an async action MovieSearch() but clueless what to do now.

Sam
  • 1,303
  • 3
  • 23
  • 41

2 Answers2

2

You have to Deserialize the JSON string they return you as responseData into c# type like Movie. For the Deserialization you can use library like JSON.NET then write your like this:

class Movie
{
   public string Name{ get; set;}
   public decimal Rating{ get; set;}
}

 string output = "{ "Name": "The Matrix", "Rating": "4.0"}"

Movie deserializedMovie = JsonConvert.DeserializeObject<Movie>(responseData);

Check what actually they return cause the response can contain not a single Movie object but is can contain a List then you have to write your code like this:

List<Movie> movies= JsonConvert.DeserializeObject<List<Movie>>(responseData);

Hope this helps :)

Aminul
  • 1,738
  • 2
  • 24
  • 42
1

Thanks to Aminuls helpful answer I found a good solution:

// SearchMovie method

public async Task MovieSearch(string search)
{          
    System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

    var baseAddress = new Uri("http://api.themoviedb.org/3/");

    using (var httpClient = new HttpClient { BaseAddress = baseAddress })
    {     
        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");

        // api_key can be requestred on TMDB website
        using (var response = await httpClient.GetAsync("search/movie?api_key=941c...&query=" + search))
        {
            string responseData = await response.Content.ReadAsStringAsync();

            var model = JsonConvert.DeserializeObject<RootObject>(responseData);

            foreach (var result in model.results)
            {
                // All movies
                // System.Diagnostics.Debug.WriteLine(result.title);
            }      
        }
    }
}

// Generated model from json2csharp.com

public class Result
{
    public bool adult { get; set; }
    public string backdrop_path { get; set; }
    public int id { get; set; }
    public string original_title { get; set; }
    public string release_date { get; set; }
    public string poster_path { get; set; }
    public double popularity { get; set; }
    public string title { get; set; }
    public bool video { get; set; }
    public double vote_average { get; set; }
    public int vote_count { get; set; }
}

public class RootObject
{
    public int page { get; set; }
    public List<Result> results { get; set; }
    public int total_pages { get; set; }
    public int total_results { get; set; }
}

// Examle of search functionaly in View

@Html.ActionLink("Search movie", "MovieSearch", new { search = "mission"})
Sam
  • 1,303
  • 3
  • 23
  • 41