将 Json 对象反序列化为 C# 对象不起作用

Deserialising JsonObject into C# object not working

我正在连接到第 3 方服务并获得类似于以下内容的 JsonObject 响应:

    [
    {"header":{"names":["test.1","test.2","test.3","test.4","test.5","test.6"]}}
    ,
    {"name":"test.1","can":"transfer?"}
    ,
    {"name":"test.2","can":"transfer?"}
    ,
    {"name":"test.3","can":"transfer?"}
    ,
    {"name":"test.4","can":"transfer?"}
    ,
    {"name":"test.5","can":"transfer?"}
    ,
    {"name":"test.6","can":"register"}
    ]

因此,使用 RestSharp 和 JsonConvert.DeserializeObject<T>(response.Content) 其中 <T> 是 RootSearch;我正在使用以下 C# 模型尝试将 json 反序列化为 C# 对象:

    public class RootSearch
    {
        public List<SearchShim> Results { get; set; }
    }


    public class SearchShim
    {
        [JsonProperty("header", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
        public SearchHeader AllUrls { get; set; }

        [JsonProperty("name", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
        public string Name { get; set; }

        [JsonProperty("can", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
        public string Can { get; set; }
    }


    public class Search
    {
        [JsonProperty("name", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
        public string Name { get; set; }

        [JsonProperty("can", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
        public string Can { get; set; }
    }

    public class SearchHeader
    {
        [JsonProperty("names", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
        public List<string> Names { get; set; }
    }

但不断出现以下错误:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'blah.RootSearch' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.

我最初尝试使用 SearchShim 看起来像:

 public class SearchShim
    {
        [JsonProperty("header", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
        public SearchHeader AllUrls { get; set; }

        public list<Search> Details { get; set; }
    }

但一直出现同样的错误。

现在我被难住了。我一定是遗漏了一些明显的东西?

将您的 SearchShim 更改为:

    public class SearchShim
    {
        [JsonProperty("header", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
        public SearchHeader AllUrls { get; set; }

        public List<Search> Searches {get;set;}

    }

您的错误是您试图反序列化为 RootSearch,而您应该反序列化为

List<SearchShim>

这是正确的对象

public class SearchShim
{
    [JsonProperty("header", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
    public SearchHeader AllUrls { get; set; }

    [JsonProperty("name", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string Name { get; set; }

    [JsonProperty("can", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string Can { get; set; }
}