타사 서비스에 연결하고 다음과 유사한 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; }
}
그러나 같은 오류가 계속 발생했습니다.
이제 난 당황해. 분명한 게 빠졌 나봐요?
귀하의 오류는 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; }
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다