为什么JSON响应即使是数组也不能反序列化为List <>?

时间:2019-05-17 05:39:16

标签: c# .net json json.net restsharp

首先,对这个漫长而冗长的问题深表歉意。我承认我也遇到过类似的问题,但是老实说,我已经尝试了所有解决方案,但并没有解决我的问题。我在C#中使用.NET进行编码,并以Newtonsoft和RestSharp作为Nu-Get程序包,而我正在尝试从Wordnik API检索数据。

这是JSON的外观:

[
  {
    "id": "A5530700-1",
    "partOfSpeech": "interjection",
    "attributionText": "from The American Heritage® Dictionary of the English Language, 5th Edition.",
    "sourceDictionary": "ahd-5",
    "text": "Used to show encouragement or approval to a boy or man.",
    "sequence": "1",
    "score": 0,
    "labels": [],
    "citations": [],
    "word": "attaboy",
    "relatedWords": [],
    "exampleUses": [
      {
        "text": "Attaboy! That's the way to hit a home run!"
      }
    ],
    "textProns": [],
    "notes": [],
    "attributionUrl": "https://ahdictionary.com/",
    "wordnikUrl": "https://www.wordnik.com/words/attaboy"
  },
  {
    "partOfSpeech": "interjection",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "Used to show encouragement or approval to a boy or man.",
    "labels": [
      {
        "text": "idiomatic",
        "type": "usage"
      },
      {
        "text": "colloquial",
        "type": "register"
      }
    ],
    "citations": [],
    "word": "attaboy",
    "relatedWords": [],
    "exampleUses": [],
    "textProns": [],
    "notes": [],
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/attaboy"
  }
]

这是我的代码中有问题的部分:

        IRestResponse response = client.Execute(request);

        var resultArray = JsonConvert.DeserializeObject<List<ResultsDefinition>>(response.Content);
        string wordDefinition = resultArray[0].Text;

这是我的ResultsDefinition类:

public class ResultsDefinition
{
    public string ExtendedText { get; set; }
    public string Text { get; set; }
    public string SourceDictionary { get; set; }
    public List<Citation> Citations { get; set; }
    public List<Label> Labels { get; set; }
    public float Score { get; set; }
    public List<ExampleUsage> ExampleUses { get; set; }
    public string AttributionUrl { get; set; }
    public string SeqString { get; set; }
    public string AttributionText { get; set; }
    public List<Related> RelatedWords { get; set; }
    public string Sequence { get; set; }
    public string Word { get; set; }
    public List<Note> Notes { get; set; }
    public List<TextPron> TextProns { get; set; }
    public string PartOfSpeech { get; set; }
}

public class Citation
{
    public string Cite { get; set; }
    public string Source { get; set; }
}

public class Label
{
    public string Text { get; set; }
    public string Type { get; set; }
}

public class ExampleUsage
{
    public string Text { get; set; }
}

public class Related
{
    public string Label1 { get; set; }
    public string RelationshipType { get; set; }
    public string Label2 { get; set; }
    public string Label3 { get; set; }
    public List<string> Words { get; set; }
    public string Gram { get; set; }
    public string Label4 { get; set; }
}

public class Note
{
    public string NoteType { get; set; }
    public List<string> AppliesTo { get; set; }
    public string Value { get; set; }
    public int Pos { get; set; }
}

public class TextPron
{
    public string Raw { get; set; }
    public int Seq { get; set; }
    public string RawType { get; set; }
}

每次我尝试在Visual Studio中运行代码时,它总是给我Newtonsoft.Json.JsonSerializationException。据我了解,这是因为JSON是以某种方式作为JsonObject而不是JsonArray读取的。

我读过here,我需要将inputString放入JsonConvert.DeserializeObject参数,但是我已经做对了(因为我在代码中写了'response.Content')?

这是我得到的确切例外:

  

Newtonsoft.Json.JsonSerializationException:'无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型'System.Collections.Generic.List`1 [ConsoleAppTest.ResultsDefinition]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。

您对此有何建议?

2 个答案:

答案 0 :(得分:0)

与我合作愉快。

看看这个例子。

enter image description here

https://dotnetfiddle.net/tZfHaZ

什么是response.Content数据类型?

如果使用双引号作为包装,请确保使用反斜杠\"来转义属性值。

如果使用单引号作为包装,请确保使用反斜杠\'来转义属性值。

答案 1 :(得分:0)

如果response.content不是字符串,请尝试以下操作:

var responseString = JsonConvert.SerializeObject(response.content);
var resultArray = JsonConvert.DeserializeObject<List<ResultsDefinition>>(responseString );
相关问题