Je me connecte à un service 3ème partie et obtenir une réponse JsonObject qui ressemble à ce qui suit:Deserialising JsonObject en C objet # ne fonctionne pas
[
{"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"}
]
Ainsi, en utilisant RestSharp et JsonConvert.DeserializeObject <T>
(réponse. Content) où <T>
est RootSearch; J'utilise les modèles suivants C# pour essayer de désérialiser le JSON dans un objet 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; }
}
Mais continuer à obtenir l'erreur suivante:
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.
Au départ, j'ai essayé avec SearchShim ressembler à:
public class SearchShim
{
[JsonProperty("header", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
public SearchHeader AllUrls { get; set; }
public list<Search> Details { get; set; }
}
Mais a continué à obtenir la même erreur.
Maintenant, je suis perplexe. Je dois manquer quelque chose d'évident?
Avez-vous essayé de définir les noms de propriété en minuscules? public string Nom {get; ensemble; } => chaîne publique name {get; ensemble; } – Gumbo
Le corps de la réponse d'un tiers est un tableau, pas un objet. Est-ce que ça marche? 'JsonConvert.DeserializeObject (response.Content [0])' –
squillman
Eh bien, non cela ne va pas fonctionner pleinement non plus. Puisque cette réponse est un tableau d'objets différents, je pense que vous allez devoir assembler manuellement le graphe d'objets en utilisant plusieurs appels à DeserializeObject() – squillman