2013-02-09 47 views
4

我一直试图让这个JSON反序列化,即使尝试了多种方法之后仍然存在问题。JSON不能反序列化JSON数组到类型

下面是由Web服务返回的JSON:

[ 
    { 
     "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" 
      ] 
     } 
     ] 
    } 
] 

非常难看,我知道。不幸的是,这是别人的网络服务,所以我可以做的不多。我一直在与C#模型类搞混,似乎无法让它工作。

调用方法:

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 

模型类:

[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; 
} 

我使用的工具,如json2csharp.com和它们所产生的同样的事情。我也玩弄了使用数组而不是List,但无济于事。另外,我也尝试用类型对象替换脚注列表(由json2sharp推荐)。

任何帮助将不胜感激。

+0

错误是'EnsureArrayContract',所以我怀疑它与类布局有关。您可能想尝试反序列化为一般的json对象,如果这种情况失败,就会在文本中消失,直到找到正在跳过解析器的字符。如果反序列化为通用对象,那么它是一个类设计问题,但您将有更多信息 – 2013-02-09 02:18:55

+0

是的。通用的JSON对象失败。我想深入一点,看看我想是怎么回事。 – pierceboggan 2013-02-09 02:43:56

回答

7

您的JSON数据被封装在一个数组,所以你需要反序列化到你的类型的数组

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

希望这有助于

+1

太棒了!这解决了问题! – pierceboggan 2013-02-17 23:10:42

0

我想你应该遵循这样的

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