【发布时间】:2021-07-29 08:49:05
【问题描述】:
我有这个问题,当我运行这个应用程序时,只有一些字段被填充,其余的保持为空? 下面是反序列化方法代码:
if (response.IsSuccessStatusCode)
{
var movies = await response.Content.ReadAsStringAsync();
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var moviesList = serializer.Deserialize<RootObject>(movies);
}
else throw new Exception(response.ReasonPhrase);
}
}
public class RootObject
{
public string status { get; set; }
public string status_message { get; set; }
public Data data { get; set; }
public Meta meta { get; set; }
}
public class Data
{
public Movie movie { get; set; }
}
public class Movie
{
public int id { get; set; }
public string url { get; set; }
public string imdb_code { get; set; }
public string title { get; set; }
public string title_english { get; set; }
public string title_long { get; set; }
public string slug { get; set; }
public int year { get; set; }
public float rating { get; set; }
public int runtime { get; set; }
public List<string> genres { get; set; }
public int download_count { get; set; }
public int like_count { get; set; }
public string description_intro { get; set; }
public string description_full { get; set; }
public string yt_trailer_code { get; set; }
public string language { get; set; }
public string mpa_rating { get; set; }
public string background_image { get; set; }
public string background_image_original { get; set; }
public string small_cover_image { get; set; }
public string medium_cover_image { get; set; }
public string large_cover_image { get; set; }
public List<Torrent> torrents { get; set; }
public string date_uploaded { get; set; }
public int date_uploaded_unix { get; set; }
}
public class Torrent
{
public string url { get; set; }
public string hash { get; set; }
public string quality { get; set; }
public string type { get; set; }
public int seeds { get; set; }
public int peers { get; set; }
public string size { get; set; }
public int size_bytes { get; set; }
public string date_uploaded { get; set; }
public int date_uploaded_unix { get; set; }
}
public class Meta
{
public int server_time { get; set; }
public string server_timezone { get; set; }
public int api_version { get; set; }
public string execution_time { get; set; }
}
唯一设置的字段是 status 和 status_message RootObject 的其他 2 个保持为空
编辑: 这是json的样子():
{ "status": "ok",
"status_message": "Query was successful",
"data": {
"movie": {
"id": 100,
"url": "https:\/\/yts.mx\/movies\/a-lot-like-love-2005",
"imdb_code": "tt0391304",
"title": "A Lot Like Love",
"title_english": "A Lot Like Love",
"title_long": "A Lot Like Love (2005)",
"slug": "a-lot-like-love-2005",
"year": 2005,
"rating": 6.6,
"runtime": 107,
"genres": [ "Action", "Comedy", "Drama", "Romance" ],
"download_count": 86626,
"like_count": 191,
"description_intro": "On a flight from Los Angeles to New York, Oliver and Emily make a connection, only to decide that they are poorly suited to be together. Over the next seven years, however, they are reunited time and time again, they go from being acquaintances to close friends to ... lovers? \u2014IMDb Editors",
"description_full": "On a flight from Los Angeles to New York, Oliver and Emily make a connection, only to decide that they are poorly suited to be together. Over the next seven years, however, they are reunited time and time again, they go from being acquaintances to close friends to ... lovers? \u2014IMDb Editors",
"yt_trailer_code": "MRPr34vEbC8",
"language": "en",
"mpa_rating": "PG-13",
"background_image": "https:\/\/yts.mx\/assets\/images\/movies\/A_Lot_Like_Love_2005\/background.jpg",
"background_image_original": "https:\/\/yts.mx\/assets\/images\/movies\/A_Lot_Like_Love_2005\/background.jpg",
"small_cover_image": "https:\/\/yts.mx\/assets\/images\/movies\/A_Lot_Like_Love_2005\/small-cover.jpg",
"medium_cover_image": "https:\/\/yts.mx\/assets\/images\/movies\/A_Lot_Like_Love_2005\/medium-cover.jpg",
"large_cover_image": "https:\/\/yts.mx\/assets\/images\/movies\/A_Lot_Like_Love_2005\/large-cover.jpg",
"torrents": [
{
"url": "https:\/\/yts.mx\/torrent\/download\/F6F33A8D26D180F7C5BA0B042D22139A782F4FDF",
"hash": "F6F33A8D26D180F7C5BA0B042D22139A782F4FDF",
"quality": "720p",
"type": "bluray",
"seeds": 1,
"peers": 4,
"size": "700.27 MB",
"size_bytes": 734286316,
"date_uploaded": "2015-10-31 21:00:11",
"date_uploaded_unix": 1446321611
},
{
"url": "https:\/\/yts.mx\/torrent\/download\/40A2F78FA8BE1834335C7927FAD775642E53FF44",
"hash": "40A2F78FA8BE1834335C7927FAD775642E53FF44",
"quality": "1080p",
"type": "bluray",
"seeds": 9,
"peers": 9,
"size": "1.40 GB",
"size_bytes": 1503238554,
"date_uploaded": "2015-10-31 21:00:15",
"date_uploaded_unix": 1446321615
}
],
"date_uploaded": "2015-10-31 21:00:11",
"date_uploaded_unix": 1446321611
}
},
"@meta": {
"server_time": 1627510627,
"server_timezone": "CET",
"api_version": 2,
"execution_time": "0 ms"
}
}
这段文字非常非常非常长这段文字非常非常非常长这段文字非常非常非常长这段文字非常非常非常长这段文字非常非常非常长这段文字非常非常非常长这段文字非常非常长很长这段文字很长很长这段文字很长很长
上面的文字只是为了绕过堆栈溢出:)
有什么帮助吗?谢谢:)
【问题讨论】:
-
json 看起来怎么样?
-
JavaScriptSerializer 是史前的。考虑 Newtonsoft.Json 或 System.Text.Json - 这还允许您使用
[JsonProperty]或[JsonPropertyName]属性装饰属性以在 json 中指定属性名称,因此您不必违反 c# 命名约定-> @ 987654328@ -
让生活变得轻松;将您的 JSON 粘贴到 quicktype.io 中,它将生成所有类和 deser 代码,并让 cmets 展示如何使用它
-
请提供minimal reproducible example。没有它,真的很难帮助你。 (我还建议使用惯用的 .NET 属性名称,然后使用属性来配置这些属性在 JSON 中的表示方式。)
-
分享你的json