2013-02-09 2 views
4

Je continue d'avoir des problèmes pour tenter de désérialiser ce fichier JSON, même après avoir essayé plusieurs approches.JSON ne peut pas désérialiser le tableau JSON dans le type

Voici le JSON qui est retourné par le service Web:

[ 
    { 
     "data":[ 
     { 
      "osis":"Matthew 5:5-8", 
      "content":"<p><span id=\"unique-id-23240\" class=\"text Matt-5-5\"><span class=\"woj\"><sup class=\"versenum\">5\u00a0<\/sup>\u201cBlessed are the <sup class='crossreference' value='(<a href=\"#cunique-id-23240A\" title=\" A\">A<\/a>)'>(<a href=\"#cunique-id-23240A\" title=\" A\">A<\/a>)<\/sup>meek, for they <sup class='crossreference' value='(<a href=\"#cunique-id-23240B\" title=\" B\">B<\/a>)'>(<a href=\"#cunique-id-23240B\" title=\" B\">B<\/a>)<\/sup>shall inherit the earth.<\/span><\/span><\/p> <p><span id=\"unique-id-23241\" class=\"text Matt-5-6\"><span class=\"woj\"><sup class=\"versenum\">6\u00a0<\/sup>\u201cBlessed are those who hunger and <sup class='crossreference' value='(<a href=\"#cunique-id-23241C\" title=\" C\">C<\/a>)'>(<a href=\"#cunique-id-23241C\" title=\" C\">C<\/a>)<\/sup>thirst <sup class='crossreference' value='(<a href=\"#cunique-id-23241D\" title=\" D\">D<\/a>)'>(<a href=\"#cunique-id-23241D\" title=\" D\">D<\/a>)<\/sup>for righteousness, for they shall be satisfied.<\/span><\/span><\/p> <p><span id=\"unique-id-23242\" class=\"text Matt-5-7\"><span class=\"woj\"><sup class=\"versenum\">7\u00a0<\/sup>\u201cBlessed are <sup class='crossreference' value='(<a href=\"#cunique-id-23242E\" title=\" E\">E<\/a>)'>(<a href=\"#cunique-id-23242E\" title=\" E\">E<\/a>)<\/sup>the merciful, for they shall receive mercy.<\/span><\/span><\/p> <p><span id=\"unique-id-23243\" class=\"text Matt-5-8\"><span class=\"woj\"><sup class=\"versenum\">8\u00a0<\/sup>\u201cBlessed are <sup class='crossreference' value='(<a href=\"#cunique-id-23243F\" title=\" F\">F<\/a>)'>(<a href=\"#cunique-id-23243F\" title=\" F\">F<\/a>)<\/sup>the pure in heart, for <sup class='crossreference' value='(<a href=\"#cunique-id-23243G\" title=\" G\">G<\/a>)'>(<a href=\"#cunique-id-23243G\" title=\" G\">G<\/a>)<\/sup>they shall see God.<\/span><\/span><\/p>", 
      "footnotes":[ 

      ], 
      "crossrefs":[ 
       "<li id=\"cunique-id-23240A\"><a href=\"#unique-id-23240\" title=\" Matthew 5:5\">Matthew 5:5<\/a> : <a href=\"\/passage\/?search=Ps 37:11&version=ESV\" data-bibleref=\"Ps.37.11\">Ps. 37:11<\/a><\/li>\n", 
       "<li id=\"cunique-id-23240B\"><a href=\"#unique-id-23240\" title=\" Matthew 5:5\">Matthew 5:5<\/a> : <a href=\"\/passage\/?search=Ps 37:11&version=ESV\" data-bibleref=\"Ps.37.11\">Ps. 37:11<\/a><\/li>\n", 
       "<li id=\"cunique-id-23241C\"><a href=\"#unique-id-23241\" title=\" Matthew 5:6\">Matthew 5:6<\/a> : <a href=\"\/passage\/?search=Ps 42:2, Isa 55:1-Isa 55:2, John 7:37&version=ESV\" data-bibleref=\"Ps.42.2,Isa.55.1-Isa.55.2,John.7.37\">Ps. 42:2; Isa. 55:1, 2; John 7:37<\/a><\/li>\n", 
       "<li id=\"cunique-id-23241D\"><a href=\"#unique-id-23241\" title=\" Matthew 5:6\">Matthew 5:6<\/a> : <a href=\"\/passage\/?search=2Tim 2:22, Matt 6:33&version=ESV\" data-bibleref=\"2Tim.2.22,Matt.6.33\">2 Tim. 2:22; [ch. 6:33]<\/a><\/li>\n", 
       "<li id=\"cunique-id-23242E\"><a href=\"#unique-id-23242\" title=\" Matthew 5:7\">Matthew 5:7<\/a> : <a href=\"\/passage\/?search=Matt 18:33, Matt 25:34-Matt 25:36, Prov 19:17, Luke 6:36, 2Tim 1:16, Heb 6:10&version=ESV\" data-bibleref=\"Matt.18.33,Matt.25.34-Matt.25.36,Prov.19.17,Luke.6.36,2Tim.1.16,Heb.6.10\">ch. 18:33; 25:34-36; Prov. 19:17; Luke 6:36; 2 Tim. 1:16; Heb. 6:10<\/a><\/li>\n", 
       "<li id=\"cunique-id-23243F\"><a href=\"#unique-id-23243\" title=\" Matthew 5:8\">Matthew 5:8<\/a> : <a href=\"\/passage\/?search=Ps 24:4, 2Tim 2:22, 1Pet 1:22&version=ESV\" data-bibleref=\"Ps.24.4,2Tim.2.22,1Pet.1.22\">Ps. 24:4; 2 Tim. 2:22; [1 Pet. 1:22]<\/a><\/li>\n", 
       "<li id=\"cunique-id-23243G\"><a href=\"#unique-id-23243\" title=\" Matthew 5:8\">Matthew 5:8<\/a> : <a href=\"\/passage\/?search=Heb 12:14, 1John 3:2-1John 3:3, Rev 22:4, 1Cor 13:12&version=ESV\" data-bibleref=\"Heb.12.14,1John.3.2-1John.3.3,Rev.22.4,1Cor.13.12\">Heb. 12:14; 1 John 3:2, 3; Rev. 22:4; [1 Cor. 13:12]<\/a><\/li>\n" 
      ] 
     }, 
     { 
      "osis":" Mark 3:9", 
      "content":"<p><span id=\"unique-id-24294\" class=\"text Mark-3-9\"><sup class=\"versenum\">9\u00a0<\/sup>And he told his disciples to <sup class='crossreference' value='(<a href=\"#cunique-id-24294A\" title=\" A\">A<\/a>)'>(<a href=\"#cunique-id-24294A\" title=\" A\">A<\/a>)<\/sup>have a boat ready for him because of the crowd, lest they <sup class='crossreference' value='(<a href=\"#cunique-id-24294B\" title=\" B\">B<\/a>)'>(<a href=\"#cunique-id-24294B\" title=\" B\">B<\/a>)<\/sup>crush him,<\/span><\/p>", 
      "footnotes":[ 

      ], 
      "crossrefs":[ 
       "<li id=\"cunique-id-24294A\"><a href=\"#unique-id-24294\" title=\" Mark 3:9\">Mark 3:9<\/a> : <a href=\"\/passage\/?search=Mark 6:32, Mark 6:45, Mark 8:10&version=ESV\" data-bibleref=\"Mark.6.32,Mark.6.45,Mark.8.10\">ch. 6:32, 45 (Gk.); 8:10 (Gk.)<\/a><\/li>\n", 
       "<li id=\"cunique-id-24294B\"><a href=\"#unique-id-24294\" title=\" Mark 3:9\">Mark 3:9<\/a> : <a href=\"\/passage\/?search=Mark 5:24, Mark 5:31&version=ESV\" data-bibleref=\"Mark.5.24,Mark.5.31\">ch. 5:24, 31<\/a><\/li>\n" 
      ] 
     } 
     ] 
    } 
] 

Je sais très laid. Malheureusement, c'est le service web de quelqu'un d'autre, donc je ne peux pas faire grand-chose. J'ai été déconner avec la classe modèle C# et n'arrive pas vraiment à le faire fonctionner.

Appel de la méthode:

2013-02-08 20:08:32.768 VersesiOS[36931:c07] Unhandled managed exception: Cannot deserialize JSON array into type 'Verses.Core.BibleGatewayVerses'. (Newtonsoft.Json.JsonSerializationException) 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract (System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue, System.String reference) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonConvert.DeserializeObject[BibleGatewayVerses] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonConvert.DeserializeObject[BibleGatewayVerses] (System.String value) [0x00000] in <filename unknown>:0 
    at Verses.Core.BibleGateway+Response.GetVerseText (System.String requestUrl) [0x00047] in /Users/pierceboggan/Desktop/Verses/Verses.Core/Verses.Core/Web Services/BibleGateway.cs:111 
    at Verses.Core.BibleGateway.GetVerseText (System.String searchKeywords) [0x00012] in /Users/pierceboggan/Desktop/Verses/Verses.Core/Verses.Core/Web Services/BibleGateway.cs:43 

Classe Modèle:

[JsonObject(MemberSerialization.OptIn)] 
public class BibleGatewayVerse 
{ 
    [JsonProperty("osis")] 
    public string Reference { get; set; } 
    [JsonProperty("content")] 
    public string Content { get; set; } 
    [JsonProperty("footnotes")] 
    public List<string> Footnotes { get; set; } 
    [JsonProperty("crossrefs")] 
    public List<string> CrossReferences { get; set; } 
} 

[JsonObject(MemberSerialization.OptIn)] 
public class BibleGatewayVerses 
{ 
    [JsonProperty("data")] 
    public List<BibleGatewayVerse> Verses; 
} 

Je l'ai utilisé des outils tels que json2csharp.com et ils génèrent la même chose. J'ai aussi joué avec l'utilisation de tableaux au lieu de List, mais en vain. En outre, j'ai également essayé de remplacer la liste de notes par l'objet type (comme recommandé par json2sharp).

Toute aide serait grandement appréciée.

+0

L'erreur est 'EnsureArrayContract', donc je doute que cela concerne la mise en page de la classe. Vous pouvez essayer de désérialiser en un objet json générique et si cela échoue, effacez le texte jusqu'à ce que vous identifiiez les caractères qui déclenchent l'analyseur. Si la désérialisation d'un objet générique fonctionne alors c'est un problème de conception de classe mais vous aurez plus d'info –

+0

Oui. L'objet JSON générique échoue. Je dois plonger un peu plus loin pour voir ce qui se passe, je suppose. – pierceboggan

Répondre

7

Vos données JSON est enveloppé dans un tableau, vous devez désérialiser à un tableau de votre type

JsonConvert.DeserializeObject<BibleGatewayVerses[]>(jsonData); 

Hope this helps

+1

Génial! Cela a résolu le problème! – pierceboggan

0

Je pense que vous devriez suivre comme ça

public class BibleGatewayVerse 
{ 

    [JsonProperty("data")] 
    public Datum[] Data { get; set; } 
} 


public class Datum 
{ 

    [JsonProperty("osis")] 
    public string Osis { get; set; } 

    [JsonProperty("content")] 
    public string Content { get; set; } 

    [JsonProperty("footnotes")] 
    public object[] Footnotes { get; set; } 

    [JsonProperty("crossrefs")] 
    public string[] Crossrefs { get; set; } 
} 
Questions connexes