2017-02-06 4 views
2

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?

+0

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

+1

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

+0

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

Répondre

2

Votre erreur est le fait que vous essayez de désérialiser dans RootSearch, au lieu que vous devez désérialiser dans

List<SearchShim> 

et c'est l'objet correct

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; } 
} 
+0

Merci Noah. Je pensais que ce serait la même chose que RootSearch, mais je suppose que cela ressemblerait à un conteneur unique pour le désérialiseur. Bien fait! – Dezzamondo

0

Changer votre SearchShim à ceci:

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

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

    } 
+0

Désolé Johan, j'ai essayé de commencer avec (bas de la question ci-dessus) et c'est toujours la même erreur – Dezzamondo

+0

Yep, désolé mate, j'ai raté cette partie, il semble que la raison pour laquelle il étouffe est due à votre json ne pas être cohérent. 'header' et le reste sont tous' name' – JohanP