【问题标题】:Deserializing JSON data (array) to C# using JSON.NET使用 JSON.NET 将 JSON 数据(数组)反序列化为 C#
【发布时间】:2015-08-03 20:11:06
【问题描述】:

您好,提前致谢!

我正在尝试反序列化 webAPI Json,但在获取数组(列表?)方面遇到了困难

我有这个来自 webAPI 的 Json,我无法编辑。

{
  "Id": "83eb701d-c280-4d39-8333-29e574898b07",
  "UserName": "silver",
  "Joined": "2015-05-14T18:42:55.14",
  "UserListedBookDtos": [
    {
      "ISBN": "9780553897845",
      "Title": "A Game of Thrones",
      "Description": "A NEW ORIGINAL SERIES.....",
      "Length": 720,
      "Author": "George R.R. Martin",
      "Status": 0
    }
  ]
}

现在我想用这个反序列化它:

        public static async Task RunAsyncGetUserBooks()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://localhost:44300/");
                var response = await client.GetAsync("api/users/"+Login.UsName+"");

                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsAsync<UserBooksResponse>();
                    MessageBox.Show(result.Id);
                }
                else
                {
                    MessageBox.Show("Something went wrong!");
                }
            }
        }

对于我使用这两个的课程:

        public class UserListedBookDtos
        {

            [JsonProperty(PropertyName = "ISBN")]
            public int ISBN { get; set; }

            [JsonProperty(PropertyName = "Title")]
            public string Title { get; set; }

            [JsonProperty(PropertyName = "Description")]
            public string Description { get; set; }

            [JsonProperty(PropertyName = "Length")]
            public int Length { get; set; }

            [JsonProperty(PropertyName = "Author")]
            public string Author { get; set; }

            [JsonProperty(PropertyName = "Status")]
            public int Status { get; set; }

        }

        public class UserBooksResponse
        {
            [JsonProperty(PropertyName = "Id")]
            public string Id { get; set; }

            [JsonProperty(PropertyName = "UserName")]
            public string UserName { get; set; }

            [JsonProperty(PropertyName = "Joined")]
            public string Joined { get; set; }

            [JsonProperty(PropertyName = "UserListedBookDtos")]
            public IList<UserListedBookDtos> UserListedBookDtos { get; set; }

        }

但是,在包含 UserBooksResponse 的 List 部分时,我似乎无法从服务器获取任何信息。但是,如果我将那部分注释掉:

        [JsonProperty(PropertyName = "UserListedBookDtos")]
        public IList<UserListedBookDtos> UserListedBookDtos { get; set; }

我收到 Id、UserName 和 Joined 没有任何问题。我对 C# 很陌生,似乎无法弄清楚是什么原因造成的。我非常感谢任何可以让我知道为什么在包含列表时它没有从服务器检索任何内容的人,或者我应该怎么做才能从列表中获取数据。

更新

我找到了第一个问题的答案,这就是为什么我根本没有检索到任何信息。这是因为我的 ISBN 是 int 而不是字符串,因为它需要是。感谢调试器提示!

【问题讨论】:

  • 你试过用 List<...> 代替 IList<...> 吗?
  • 还可以尝试注释掉 UserListedBookDtos 类的每个属性,看看问题确实出在数组上还是仅出在数组中类的一个属性上
  • 尝试使用调试器并逐步进入您的代码并检查值
  • 是的,我做到了,不幸的是结果是一样的。我可能错了,但据我所知,列表 不能包含“aray”,我认为我的 json UserListedBookDtos 部分是?

标签: c# json serialization


【解决方案1】:

问题是您将ISBN 属性映射为int,但值9780553897845 对于32 位整数来说太大了。它可能适合 long (Int64),但您可能应该将其映射为 string,因为它不是真正的数字(它只是恰好由数字组成的标识符)。

【讨论】:

  • 感谢您的回答,确实,这是我第一个问题的罪魁祸首,我设法在几秒钟前解决了它。我的第二面墙还在,但似乎没有找到从数组本身保存信息的方法。我认为这是因为我没有在 var result = await response.Content.ReadAsAsync(); 中正确保存信息。或者根本没有检索到信息。
  • @svanamet,我不明白你的意思。我尝试使用您的类(在 ISBN 上进行修复)反序列化您的 JSON 示例,它工作正常。什么不完全有效?
  • 我正在尝试从 List 中检索信息,如标题、说明、长度、作者和状态到 Datagridview 但字段显示为空。我想我要么反序列化错误,要么我的 datagridview 设置不当。
  • @svanamet,在 LinqPad 中的快速测试显示数据已正确反序列化,因此问题一定出在其他地方。
  • 感谢您占用您的时间,并为此道歉!现在我知道我需要从其他地方看。
猜你喜欢
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
相关资源
最近更新 更多