JSON无法将JSON数组反序列化为类型

3

我一直在尝试解析这个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,但是没有成功。此外,我还尝试将Footnotes List替换为类型对象(正如json2sharp所建议的)。如果有任何帮助,将不胜感激。

错误信息是 'EnsureArrayContract',所以我不认为它与类的布局有关。您可以尝试将其反序列化为通用的 JSON 对象,如果失败,请逐步删除文本,直到找到导致解析器失败的字符。如果反序列化为通用对象可行,则可能是类设计问题,但您会获得更多信息。 - Miserable Variable
是的。通用的 JSON 对象出了问题。我想我得深入一点去看看发生了什么。 - pierceboggan
2个回答

8

您的Json数据被包装在一个数组中,因此您需要将其反序列化为您类型的数组。

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

希望这有所帮助。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接