使用 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 的列表部分时,我似乎无法从服务器获取任何信息。但是,如果我评论那部分:
[JsonProperty(PropertyName = "UserListedBookDtos")]
public IList<UserListedBookDtos> UserListedBookDtos { get; set; }
我毫无问题地收到了 Id、UserName 和 Joined。我对 C# 很陌生,似乎无法弄清楚是什么原因造成的。如果有人可以让我知道为什么在包含列表时它没有从服务器检索任何内容,或者我应该怎么做才能从列表中获取数据,我将不胜感激。
更新
我找到了第一个问题的答案,这也是我没有检索到任何信息的原因。这是因为我的 ISBN 是 int 而不是字符串,因为它需要是。感谢调试器提示!
问题是您将 ISBN
属性 映射为 int
,但值 9780553897845 对于 32 位整数来说太大了。它可以放在 long
(Int64
) 中,但您可能应该将其映射为 string
,因为它实际上不是一个数字(它只是一个标识符,恰好只是位数)。
嘿,提前致谢!
我正在尝试反序列化 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 的列表部分时,我似乎无法从服务器获取任何信息。但是,如果我评论那部分:
[JsonProperty(PropertyName = "UserListedBookDtos")]
public IList<UserListedBookDtos> UserListedBookDtos { get; set; }
我毫无问题地收到了 Id、UserName 和 Joined。我对 C# 很陌生,似乎无法弄清楚是什么原因造成的。如果有人可以让我知道为什么在包含列表时它没有从服务器检索任何内容,或者我应该怎么做才能从列表中获取数据,我将不胜感激。
更新
我找到了第一个问题的答案,这也是我没有检索到任何信息的原因。这是因为我的 ISBN 是 int 而不是字符串,因为它需要是。感谢调试器提示!
问题是您将 ISBN
属性 映射为 int
,但值 9780553897845 对于 32 位整数来说太大了。它可以放在 long
(Int64
) 中,但您可能应该将其映射为 string
,因为它实际上不是一个数字(它只是一个标识符,恰好只是位数)。