【问题标题】:c# json Deserialize not fill all the data in the classc# json反序列化不填充类中的所有数据
【发布时间】: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

标签: c# json api


【解决方案1】:

@CaiusJard 我试过了,没有运气数据字段和元字段仍然为空

那你肯定对我做了不一样的事:

答:

我把你的 json 保存到一个名为 some.json 的文件中,并将其设置为复制到输出目录(这样我就可以轻松加载它)。我写了一行代码来加载它

乙:

我按照 cmets 中的建议访问了 quicktype.io 并将您的 json 粘贴到那里,它给了我一些不错的 C# 类以及关于如何使用它的评论;我只是把它粘贴到我的 Program.cs 的末尾

C:

我在我的程序的注释中添加了using 和代码行。


我没有安装 Newtonsoft。评论说要安装 newtonsoft。最简单的方法是滚动到底部并指向 newtonsoft 类名下的任何错误;它非常受欢迎,以至于 Visual Studio 知道其类的名称,并会提供为您安装“单击灯泡以帮助修复此错误”:

D:

安装后,我在代码上设置了一个断点并运行它。我越过了加载 json 的行,现在您可以在底部的 Locals 窗口中看到 someThing 确实填充了数据,包括 Data 和 Meta


全部完成,只需要 3 行代码,而且我只需要编写其中的一行 - 奖励! :D

【讨论】:

  • 我与 QuickType.io 没有任何关系
  • 是的,非常感谢
  • 没问题。顺便说一句,我们说谢谢的方式是单击向上箭头和问题旁边的灰色复选标记..
猜你喜欢
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多