Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JSON.NET将JSON数据(数组)反序列化为C#_C#_Json_Serialization - Fatal编程技术网

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

使用JSON.NET将JSON数据(数组)反序列化为C#,c#,json,serialization,C#,Json,Serialization,嘿,提前谢谢你 我正在尝试反序列化webAPI Json,但在获取数组(列表?)方面遇到了困难 我有来自webAPI的Json,无法编辑 { "Id": "83eb701d-c280-4d39-8333-29e574898b07", "UserName": "silver", "Joined": "2015-05-14T18:42:55.14", "UserListedBookDtos": [ { "ISBN": "9780553897845",

嘿,提前谢谢你

我正在尝试反序列化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!");
                }
            }
        }
公共静态异步任务RunAsyncGetUserBooks()
{
使用(var client=new HttpClient())
{
client.BaseAddress=新Uri(“https://localhost:44300/");
var response=wait client.GetAsync(“api/users/”+Login.UsName+”);
if(响应。IsSuccessStatusCode)
{
var result=await response.Content.ReadAsAsync();
MessageBox.Show(result.Id);
}
其他的
{
MessageBox.Show(“出错了!”);
}
}
}
对于类,我使用以下两种:

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

        }
public类UserListedBookDtos
{
[JsonProperty(PropertyName=“ISBN”)]
公共整数ISBN{get;set;}
[JsonProperty(PropertyName=“Title”)]
公共字符串标题{get;set;}
[JsonProperty(PropertyName=“Description”)]
公共字符串说明{get;set;}
[JsonProperty(PropertyName=“Length”)]
公共整数长度{get;set;}
[JsonProperty(PropertyName=“Author”)]
公共字符串作者{get;set;}
[JsonProperty(PropertyName=“Status”)]
公共int状态{get;set;}
}
公共类用户响应
{
[JsonProperty(PropertyName=“Id”)]
公共字符串Id{get;set;}
[JsonProperty(PropertyName=“UserName”)]
公共字符串用户名{get;set;}
[JsonProperty(PropertyName=“Joined”)]
公共字符串{get;set;}
[JsonProperty(PropertyName=“UserListedBookDtos”)]
公共IList UserListedBookTos{get;set;}
}
然而,当包含UserBooksResponse的列表部分时,我似乎无法从服务器获取任何信息。但是,如果我将该部分注释掉:

        [JsonProperty(PropertyName = "UserListedBookDtos")]
        public IList<UserListedBookDtos> UserListedBookDtos { get; set; }
[JsonProperty(PropertyName=“UserListedBookDtos”)]
公共IList UserListedBookTos{get;set;}
我收到的Id,用户名和加入没有任何问题。我对C#很陌生,似乎不知道是什么原因造成的。我非常感谢任何人能告诉我为什么它在包含列表时没有从服务器检索任何内容,或者我应该做什么才能从列表中获取数据

更新


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

问题是您正在将
ISBN
属性映射为
int
,但是值9780553897845对于32位整数来说太大了。它可以放在一个
长的
Int64
)中,但是你可能应该把它映射成一个
字符串,因为它不是一个真正的数字(它只是一个刚好由数字组成的标识符)。

你试过用List而不是IList吗?也试着注释你的UserListedBookDtos类的每个属性,要查看问题是否确实存在于数组中,或者只是存在于ArrayTrey中类的一个属性中,请使用调试器并逐步进入代码并检查值。很遗憾,结果是相同的。我可能错了,但据我所知,列表中不能包含“aray”,我认为我的json UserListedBookDtos部分是?谢谢你的回答,事实上,这是我第一个问题的罪魁祸首,我在几秒钟前就解决了。虽然我的第二堵墙还没修好,但似乎没有找到从数组本身保存信息的方法。我认为这是因为我没有在var result=await response.Content.ReadAsAsync()中正确保存信息;或者根本没有检索到信息。@svanamet,我不明白你的意思。我尝试使用您的类反序列化您的JSON示例(修复了ISBN),效果很好。什么不起作用?我正试图从列表中检索信息,如标题、说明、长度、作者和状态到Datagridview,但字段显示为空。我想我要么反序列化错误,要么我的datagridview设置不好。@svanamet,LinqPad中的快速测试表明数据已正确反序列化,因此问题一定出在其他地方。感谢您,并为占用您的时间表示歉意!现在我知道我需要从别的地方看。