首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么JSON响应不能反序列化为List<>,即使它是一个数组?

为什么JSON响应不能反序列化为List<>,即使它是一个数组?
EN

Stack Overflow用户
提问于 2019-05-17 05:39:15
回答 2查看 143关注 0票数 0

首先,对于这个冗长的,也许是多余的问题,我很抱歉。我承认我看到过一个类似的问题,但我诚实地尝试了所有的解决方案,但它并没有解决我的问题。我使用.NET在C#中编写代码,使用Newtonsoft和RestSharp作为我的Nu-Get包,并试图从Wordnik中检索数据。

JSON的外观如下:

代码语言:javascript
复制
[
  {
    "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"
  }
]

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

代码语言:javascript
复制
        IRestResponse response = client.Execute(request);

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

这是我的ResultsDefinition类:

代码语言:javascript
复制
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; }
}

每次我试图在VisualStudio中运行我的代码时,它总是给我Newtonsoft.Json.JsonSerializationException。据我理解,这是因为JSON在某种程度上被解读为JsonObject而不是JsonArray。

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

这正是我得到的例外:

'System.Collections.Generic.List`1ConsoleAppTest.ResultsDefinition‘:’无法将当前JSON对象(例如,{“名称”:“值”})反序列化为类型Newtonsoft.Json.JsonSerializationException,因为该类型需要一个JSON数组(例如,1、2、3)才能正确反序列化。

你有什么建议来解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2019-05-17 06:13:25

跟我合作得很好。

看看这个样本。

https://dotnetfiddle.net/tZfHaZ

什么是response.Content数据类型?

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

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

票数 0
EN

Stack Overflow用户

发布于 2019-05-17 06:24:17

如果response.content不是字符串,那么尝试如下:

代码语言:javascript
复制
var responseString = JsonConvert.SerializeObject(response.content);
var resultArray = JsonConvert.DeserializeObject<List<ResultsDefinition>>(responseString );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56180248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档