在C#中从OMDB获取电影信息(请显示示例) [英] Grab movie info from OMDB in C# (please show an example)
问题描述
我问了一个关于如何从互联网上获取有关电影的信息的问题......有人说只使用网络服务方法并以JSON格式获得输出 - 所以我想知道如何获得输出?例如:我的软件有5-4个文本框,它有一个带按钮的搜索框 - 所以我希望我的软件获取有关我正在搜索的任何内容的信息,并用这些信息填充这些文本框。就像我搜索钢铁侠软件从omdb获得年(如2015)并将2015年放在年份文本框上。我是C#的新手 - 你能告诉我一个关于如何做的代码示例吗?
http://www.omdbapi.com/?t=Iron%20man [ ^ ]
我尝试过:
我问了一个关于如何从互联网上获取有关电影的信息的问题......有人说只需使用网络服务方法并以JSON格式获得输出 - 所以我想知道如何获得输出?
I asked a question about how to get information about a movie from the internet... and someone said "Just use web service methods and get output in JSON Format" - so I want to know how to get output? For example: my software has 5-4 textboxes and it has a search box with a button - so I want my software get info about anything that I'm searching and fill those text boxes with that info. Like when I searched "Iron man" the software get "year(like 2015)" from omdb and put that 2015 on "year textbox". I'm new in C# - can you please show me a code example about how to do that?
http://www.omdbapi.com/?t=Iron%20man[^]
What I have tried:
I asked a question about how to get information about a movie from the internet... and someone said "Just use web service methods and get output in JSON Format" - so I want to know how to get output?
推荐答案
试试这个
创建一个这样的类
try this
Create a class like this
public class ImdbEntity
{
public string Title { get; set; }
public string Year { get; set; }
public string Rated { get; set; }
public string Released { get; set; }
public string Runtime { get; set; }
public string Genre { get; set; }
public string Director { get; set; }
public string Writer { get; set; }
public string Actors { get; set; }
public string Plot { get; set; }
public string Language { get; set; }
public string Country { get; set; }
public string Awards { get; set; }
public string Poster { get; set; }
public string Metascore { get; set; }
public string imdbRating { get; set; }
public string imdbVotes { get; set; }
public string imdbID { get; set; }
public string Type { get; set; }
public string Response { get; set; }
}
使用以下代码将电影信息的值填充到相应的文本框中,
根据您的需要自定义它。
use the below code to fill the values of the movie information into the respective text-boxes,
customize it based on your need.
private void btnSearch_Click(object sender, EventArgs e)
{
string url = "http://www.omdbapi.com/?t=" + txtMovieName.Text.Trim();
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString(url);
JavaScriptSerializer oJS = new JavaScriptSerializer();
ImdbEntity obj = new ImdbEntity();
obj = oJS.Deserialize<ImdbEntity>(json);
if (obj.Response == "True")
{
txtActor.Text = obj.Actors;
txtDirector.Text = obj.Director;
txtYear.Text = obj.Year;
}
else
{
MessageBox.Show("Movie not Found!!!");
}
}
}
注意:将此dll( System.Web.Extensions.dll
)引用到您的项目
Note: Refer this dll (System.Web.Extensions.dll
) to your project
using System.Web.Script.Serialization;
这篇关于在C#中从OMDB获取电影信息(请显示示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!