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

标签 c# json-deserialization

我正在连接到第 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> (响应.内容)其中 <T>是根搜索;我正在使用以下 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; }
    }

但不断收到相同的错误。

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

最佳答案

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

关于c# - 将 Json 对象反序列化为 C# 对象不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42078386/

相关文章:

c# - 我想使用 C# 以表格格式通过电子邮件发送存储在本地的屏幕截图?

C# 如何以编程方式获取SQL Server安装路径?

c# - 从 UTC 转换为本地时,夏令时在 TimeZoneInfo 中不起作用

c# - 给定用户名和密码,您如何模拟提升的用户?

c# - 使用 JsonConverter 的 OnDeserialized 回调

java - 为什么在这个语句中使用{}来实例化,Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();

c# - 如何避免在OnTriggerEnter()中调用GetComponent()?

xml - 强制 JacksonXML 将原始 XML 反序列化为字符串

c# - 无法将 HttpResponseMessage 反序列化为模型对象

java - Jackson:在不提供 id 的情况下无法明确指示多态性中的目标类?