将JsonObject反序列化为C#对象不起作用
我正在连接到一个第三方服务,并得到一个类似于以下内容的JsonObject响应:将JsonObject反序列化为C#对象不起作用,c#,json-deserialization,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","ca
[
{"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
(response.Content),其中
是根搜索;我使用以下C#模型尝试将json反序列化为C#对象:
公共类根搜索
{
公共列表结果{get;set;}
}
公共类搜索引擎
{
[JsonProperty(“标头”,NullValueHandling=NullValueHandling.Ignore,DefaultValueHandling=DefaultValueHandling.Ignore)]
公共搜索头AllUrls{get;set;}
[JsonProperty(“名称”,NullValueHandling=NullValueHandling.Ignore,DefaultValueHandling=DefaultValueHandling.Ignore)]
公共字符串名称{get;set;}
[JsonProperty(“can”,NullValueHandling=NullValueHandling.Ignore,DefaultValueHandling=DefaultValueHandling.Ignore)]
公共字符串可以{get;set;}
}
公共类搜索
{
[JsonProperty(“名称”,NullValueHandling=NullValueHandling.Ignore,DefaultValueHandling=DefaultValueHandling.Ignore)]
公共字符串名称{get;set;}
[JsonProperty(“can”,NullValueHandling=NullValueHandling.Ignore,DefaultValueHandling=DefaultValueHandling.Ignore)]
公共字符串可以{get;set;}
}
公共类搜索头
{
[JsonProperty(“名称”,NullValueHandling=NullValueHandling.Ignore,DefaultValueHandling=DefaultValueHandling.Ignore)]
公共列表名称{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.
无法将当前JSON数组(例如[1,2,3])反序列化为类型“blah.RootSearch”,因为该类型需要一个JSON对象(例如{“name”:“value”})才能正确反序列化。
要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”}),或将反序列化类型更改为数组或实现可从JSON数组反序列化的集合接口(例如ICollection、IList)类似列表的类型。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化。
路径“”,第1行,位置1。
我最初尝试使用SearchShim,看起来像:
public class SearchShim
{
[JsonProperty("header", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
public SearchHeader AllUrls { get; set; }
public list<Search> Details { get; set; }
}
公共类搜索
{
[JsonProperty(“标头”,NullValueHandling=NullValueHandling.Ignore,DefaultValueHandling=DefaultValueHandling.Ignore)]
公共搜索头AllUrls{get;set;}
公共列表详细信息{get;set;}
}
但却不断地犯同样的错误
现在我被难倒了。我一定是遗漏了什么明显的东西?将您的
搜索垫片更改为:
public class SearchShim
{
[JsonProperty("header", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
public SearchHeader AllUrls { get; set; }
public List<Search> Searches {get;set;}
}
公共类搜索
{
[JsonProperty(“标头”,NullValueHandling=NullValueHandling.Ignore,DefaultValueHandling=DefaultValueHandling.Ignore)]
公共搜索头AllUrls{get;set;}
公共列表搜索{get;set;}
}
您的错误是您试图反序列化到RootSearch,而应该反序列化到
List<SearchShim>
您是否尝试将属性名称设置为小写?公共字符串名称{get;set;}=>公共字符串名称{get;set;}第三方响应主体是数组,而不是对象。这行吗<代码>JsonConvert.DeserializeObject(response.Content[0])
嗯,不,这也不会完全起作用。由于该响应是一个不同对象的数组,我认为您必须通过多次调用DeserializeObject()@Gumbo-Yep手动将对象图拼凑在一起,JsonProperty属性用于从JSON结果中获取小写名称。@squillman-Arrgh!我以为你对你的建议非常赞同。结果是响应。内容[0]在我们接近它之前抛出了一个编译器错误。我尝试过向RootSearch添加一个[JsonArray]属性,但也没有任何效果:(对不起,Johan,我试着从(上面问题的底部)开始)它仍然是相同的错误是的,抱歉,伙计,我错过了这一部分。看起来它阻塞的原因是由于您的json不一致。第一项是标题,其余的都是名称。谢谢Noah。我本以为它与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; }
}