繁体   English   中英

使用JSON.NET将JSON数据(数组)反序列化为C#

[英]Deserializing JSON data (array) to C# using JSON.NET

嘿,谢谢你!

我正在尝试反序列化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#的新手,似乎无法弄清楚是什么原因造成的。 我将不胜感激,任何人都可以让我知道为什么在包含列表时为什么它没有从服务器检索任何内容,或者我也应该怎么做才能从列表中获取数据。

UPDATE

我找到了第一个问题的答案,这就是为什么我什么都找不到的原因。 这是因为我的ISBN是int而不是需要的字符串。 感谢调试器提示!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM