1

如何让用户在 ASP.NET MVC 5 应用程序中使用 TMDB API (The Movie Db) 搜索电影并返回 JSON 结果。

使用我的个人 api 密钥并返回包含字符串“任务”的所有电影的 json 结果在 VB 之外的工作示例:

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

文档(http://docs.themoviedb.apiary.io/#reference/search/searchmovie)建议对 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();
    }
}

我将代码粘贴到异步操作 MovieSearch() 中,但不知道现在该做什么。

4

2 回答 2

2

您必须将它们返回给您的 JSON 字符串反序列responseData化为 c# 类型,例如Movie. 对于反序列化,您可以使用JSON.NET 之类的库,然后像这样编写:

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);

检查它们实际返回的内容,因为响应不能包含单个 Movie 对象,但可以包含 List,那么您必须像这样编写代码:

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

希望这可以帮助 :)

于 2015-05-13T08:35:28.987 回答
1

感谢 Aminuls 有用的回答,我找到了一个很好的解决方案:

// 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"})
于 2015-05-13T09:14:02.013 回答