gpt4 book ai didi

C# TheMovieDB serializer.Deserialize 返回 0 条记录

转载 作者:太空宇宙 更新时间:2023-11-03 22:42:20 25 4
gpt4 key购买 nike

我正在尝试使用 C# 从 TheMovieDB API 检索数据。我现在不想使用外部库,因为我想在 C# 方面做得更好。

TheMovieDB API 返回以下内容:

Enter image description here

我有以下类(class):

    public class raizDoJson
{
public int pagina { get; set; }
public int totalRegistros { get; set; }
public int totalPaginas { get; set; }
public List<serie> series { get; set; }
}

public class serie
{
public string original_name { get; set; }
public int id { get; set; }
public string name { get; set; }
public int vote_count { get; set; }
public double vote_average { get; set; }
public string poster_path { get; set; }
public string first_air_date { get; set; }
public int popularity { get; set; }
public List<generos> genre_ids { get; set; }
public string original_language { get; set; }
public string backdrop_path { get; set; }
public string overview { get; set; }
public List<paises> origin_country { get; set; }
}

public class generos{
public int id { get; set; }
}

public class paises {
public string pais { get; set; }
}

接下来我尝试反序列化它:

    public static void lerDados(string query)
{
string api = " API KEY";
string endereco = "http://api.themoviedb.org/3/search/tv?api_key={0}&query={1}";
string url = HttpUtility.UrlDecode(string.Format(endereco, api, query));

Console.WriteLine(url);
Console.ReadKey();

var request = System.Net.WebRequest.Create(url) as System.Net.WebRequest;
request.Method = "GET";
request.ContentLength = 0;

string conteudo;
using (var resposta = request.GetResponse() as System.Net.HttpWebResponse)
{
using (var leitor = new System.IO.StreamReader(resposta.GetResponseStream()))
{
conteudo = leitor.ReadToEnd();
//var estrutura = new JavaScriptSerializer().Deserialize<raizDoJson>(conteudo);
var serializer = new JavaScriptSerializer();
var estrutura = serializer.Deserialize<raizDoJson>(conteudo);
}
}
}

最后,conteudo 得到这个值:

{
"page": 1,
"total_results": 2,
"total_pages": 1,
"results": [
{
"original_name": "The Big Bang Theory",
"id": 1418,
"name": "The Big Bang Theory",
"vote_count": 3100,
"vote_average": 6.82,
"poster_path": "\/ooBGRQBdbGzBxAVfExiO8r7kloA.jpg",
"first_air_date": "2007-09-24",
"popularity": 319.823,
"genre_ids": [
35
],
"original_language": "en",
"backdrop_path": "\/nGsNruW3W27V6r4gkyc3iiEGsKR.jpg",
"overview": "The Big Bang Theory is centered on five characters living in Pasadena, California: roommates Leonard Hofstadter and Sheldon Cooper; Penny, a waitress and aspiring actress who lives across the hall; and Leonard and Sheldon's equally geeky and socially awkward friends and co-workers, mechanical engineer Howard Wolowitz and astrophysicist Raj Koothrappali. The geekiness and intellect of the four guys is contrasted for comic effect with Penny's social skills and common sense.",
"origin_country": [
"US"
]
},
{
"original_name": "The Big Bang",
"id": 20261,
"name": "The Big Bang",
"vote_count": 2,
"vote_average": 5.5,
"poster_path": "\/9ZQX9aujbfd3N4FMVf16FwLRg0K.jpg",
"first_air_date": "1996-04-15",
"popularity": 0.079,
"genre_ids": [
9648
],
"original_language": "en",
"backdrop_path": null,
"overview": "The Big Bang is a CITV science show that broadcast from April 15, 1996 - September 8, 2004, produced by Yorkshire Television. It is notable for being one of CITV's longest-running science programmes. The aim of the programme was to make science fun and interesting for children.",
"origin_country": [
"GB"
]
}
]
}

但是 estrutura 没有记录...请问,我做错了什么?

最佳答案

显示的类与 JSON 不匹配。

更新您的类以匹配从 API 返回的数据

public class raizDoJson {
public int page { get; set; }
public int total_results { get; set; }
public int total_pages { get; set; }
public List<resultado> results { get; set; }
}

public class resultado {
public string original_name { get; set; }
public int id { get; set; }
public string name { get; set; }
public int vote_count { get; set; }
public double vote_average { get; set; }
public string poster_path { get; set; }
public string first_air_date { get; set; }
public int popularity { get; set; }
public List<string> genre_ids { get; set; }
public string original_language { get; set; }
public string backdrop_path { get; set; }
public string overview { get; set; }
public List<string> origin_country { get; set; }
}

关于C# TheMovieDB serializer.Deserialize 返回 0 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51604184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com