无法将当前 JSON 对象反序列化为类型 'System.Collections.Generic.List,因为该类型需要 JSON 数组才能正确反序列化

Cannot deserialize the current JSON objectinto type 'System.Collections.Generic.List because the type requires a JSON arrayto deserialize correctly

我正在使用 Imgur API 并且在反序列化 JSON 响应时遇到了问题。我的 JSON 看起来像这样:

{
  "data":{
    "id":"zPBszkd",
    "title":null,
    "description":null,
    "datetime":1634033771,
    "type":"image\/png",
    "animated":false,
    "width":391,
    "height":149,
    "size":11093,
    "views":0,
    "bandwidth":0,
    "vote":null,
    "favorite":false,
    "nsfw":null,
    "section":null,
    "account_url":null,
    "account_id":0,
    "is_ad":false,
    "in_most_viral":false,
    "has_sound":false,
    "tags":[
      
    ],
    "ad_type":0,
    "ad_url":"",
    "edited":"0",
    "in_gallery":false,
    "deletehash":"EUqn23MqOTdDOov",
    "name":"",
    "link":"https:\/\/i.imgur.com\/zPBszkd.png"
  },
  "success":true,
  "status":200
}

但我不确定如何使用 Newtonsoft 对其进行反序列化。我已经阅读了一些指南和问题,但仍然 运行 陷入同样的​​错误:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.String]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,也不是像数组或列表这样的集合类型)可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型以强制它从 JSON 对象反序列化。

希望能提供正确反序列化 JSON 的代码片段,并解释为什么必须以某种方式完成

为了转换任何 json,我建议使用这个网站:https://json2csharp.com/ 只需复制您的 json 并基于它创建一个 c# class

要解决您的问题,请先创建一个模型 class :

    public class Data 
{
    public string id { get; set; }
    public object title { get; set; }
    public object description { get; set; }
    public int datetime { get; set; }
    public string type { get; set; }
    public bool animated { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public int size { get; set; }
    public int views { get; set; }
    public int bandwidth { get; set; }
    public object vote { get; set; }
    public bool favorite { get; set; }
    public object nsfw { get; set; }
    public object section { get; set; }
    public object account_url { get; set; }
    public int account_id { get; set; }
    public bool is_ad { get; set; }
    public bool in_most_viral { get; set; }
    public bool has_sound { get; set; }
    public List<object> tags { get; set; }
    public int ad_type { get; set; }
    public string ad_url { get; set; }
    public string edited { get; set; }
    public bool in_gallery { get; set; }
    public string deletehash { get; set; }
    public string name { get; set; }
    public string link { get; set; }
}

public class Root
{
    public Data data { get; set; }
    public bool success { get; set; }
    public int status { get; set; }
}

这是你的转义 json 转换为字符串:

var str="{\"data\":{\"id\":\"zPBszkd\",\"title\":null,\"description\":null,\"datetime\":1634033771,\"type\":\"image\\/png\",\"animated\":false,\"width\":391,\"height\":149,\"size\":11093,\"views\":0,\"bandwidth\":0,\"vote\":null,\"favorite\":false,\"nsfw\":null,\"section\":null,\"account_url\":null,\"account_id\":0,\"is_ad\":false,\"in_most_viral\":false,\"has_sound\":false,\"tags\":[],\"ad_type\":0,\"ad_url\":\"\",\"edited\":\"0\",\"in_gallery\":false,\"deletehash\":\"EUqn23MqOTdDOov\",\"name\":\"\",\"link\":\"https:\\/\\/i.imgur.com\\/zPBszkd.png\"},\"success\":true,\"status\":200}\r\n"

现在您可以使用 Newtonsoft 反序列化它了:

JsonConvert.DeserializeObject<Root>(str);

如果您不确定将给定的 JSON 字符串反序列化为正确的 class 结构,您可以使用 Json2CSharp 或 Visual Studios Paste Special 功能将为您生成 class 结构:

public class Data
{
    public string id { get; set; }
    public object title { get; set; }
    public object description { get; set; }
    public int datetime { get; set; }
    public string type { get; set; }
    public bool animated { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public int size { get; set; }
    public int views { get; set; }
    public int bandwidth { get; set; }
    public object vote { get; set; }
    public bool favorite { get; set; }
    public object nsfw { get; set; }
    public object section { get; set; }
    public object account_url { get; set; }
    public int account_id { get; set; }
    public bool is_ad { get; set; }
    public bool in_most_viral { get; set; }
    public bool has_sound { get; set; }
    public List<object> tags { get; set; }
    public int ad_type { get; set; }
    public string ad_url { get; set; }
    public string edited { get; set; }
    public bool in_gallery { get; set; }
    public string deletehash { get; set; }
    public string name { get; set; }
    public string link { get; set; }
}

public class Root
{
    public Data data { get; set; }
    public bool success { get; set; }
    public int status { get; set; }
}

有了正确的 class 结构,您现在可以将 JSON 字符串反序列化为模型的对象:

var result = JsonConvert.DeserializeObject<Root>(json);