svanamet
svanamet

Reputation: 43

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

Hey and thanks in advance!

I'm trying to deserialize a webAPI Json but struggling on getting the array (list?)

I have this Json from webAPI which i can't edit.

{
  "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
    }
  ]
}

Now i am trying to deserialize it with this:

        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!");
                }
            }
        }

For classes i'm using these two:

        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; }

        }

However i can not seem to get any information from server what so ever while having List part of UserBooksResponse included. However, if i comment that part out:

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

I receive Id, UserName and Joined without any problems. I'm quite new to C# and can't seem to figure out what's causing this. I would highly appreciate anyone who could either let me know why it isn't retrieving anything from the server while the list is included, or what should i do in order to get the data from the list as well.

UPDATE

I found the answer for my first question, as is why i retrieved no information at all. It was caused because my ISBN was int and not a string, as it needed to be. Thanks for the debugger tip!

Upvotes: 2

Views: 116

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292655

The problem is that you're mapping the ISBN property as int, but the value 9780553897845 is much too large for a 32-bit integer. It could fit in a long (Int64), but you should probably map it as a string instead, because it's not really a number (it's an identifier that just happens to be made only of digits).

Upvotes: 3

Related Questions