将JsonObject反序列化为C#对象无法正常工作

时间:2017-02-06 22:24:32

标签: c# json-deserialization

我正在连接到第三方服务并获得类似于以下内容的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; }
    }

但是仍然遇到同样的错误。

现在我很难过。我一定错过了一些明显的东西吗?

2 个答案:

答案 0 :(得分:2)

您的错误是您尝试反序列化为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; }
}

答案 1 :(得分:0)

将您的SearchShim更改为:

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

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

    }