使用 NewtonSoft 反序列化 List<Object>。无法转换 IList

Deserialize List<Object> using NewtonSoft. Cannot convert IList

我正在尝试解析评论对象列表 from here. A comment object is a class in the leankit namespace: LeanKit.API.Client.Library.TransferObjects.Comment,但我在下面块的最后一行特别是在 responseString:

cannot convert from 'System.Collections.Generic.IList' to string

为什么我会收到这个?我正在指定我专门为反序列化列表而创建的自定义 class:

public class MyCommentList
{
    public string ReplyText { get; set; }
    public List<Comment> ReplyData { get; set; }
    public string ReplyCode { get; set; }
} 

调用 class

var url = "https://" + acctName + ".leankit.com/kanban/api/card/getcomments/" + boardid + "/" + cardid;
var responseString = await url.WithBasicAuth("xxx", "yyy").GetJsonListAsync();
MyCommentList mycomment = JsonConvert.DeserializeObject<MyCommentList>(responseString);

调用 class(使用 Flurl)的更简洁版本:

var url = "https://" + acctName + ".leankit.com/kanban/api/card/getcomments/" + boardid + "/" + cardid;
MyCommentList mycomment = await url.WithBasicAuth("xxx", "yyy").GetAsync().ReceiveJson<MyCommentList>();

转载于此的JSON结构(来自上面的link):

{
  "ReplyData": [
    [
      {
        "Id": 256487698,
        "Text": "First comment for this card.",
        "TaggedUsers": null,
        "PostDate": "10/14/2015 at 04:36:02 PM",
        "PostedByGravatarLink": "3ab1249be442027903e1180025340b3f",
        "PostedById": 62984826,
        "PostedByFullName": "David Neal",
        "Editable": true
      }
    ]
  ],
  "ReplyCode": 200,
  "ReplyText": "Card comments successfully retrieved."
}

在JSON中,"ReplyData"是一个二维锯齿状数组:

{
  "ReplyData": [ [ ... ] ],
}

在您的模型中,它是一维列表:

public List<Comment> ReplyData { get; set; }.  

您需要将其更改为 public List<List<Comment>> ReplyData { get; set; } 以反映实际 JSON:

public class MyCommentList
{
    public string ReplyText { get; set; }
    public List<List<Comment>> ReplyData { get; set; }
    public string ReplyCode { get; set; }
} 

我假设 Comment 取自 https://github.com/LeanKit/LeanKit.API.Client/blob/master/LeanKit.API.Client.Library/TransferObjects/Comment.cs

如果有可能它有时是一维数组有时是二维数组,您可能需要从 this answer to How to handle both a single item and an array for the same property using JSON.net by Brian Rogers 应用 SingleOrArrayConverter<Comment>,如下所示:

public class MyCommentList
{
    public string ReplyText { get; set; }

    [JsonProperty(ItemConverterType = typeof(SingleOrArrayConverter<Comment>))]
    public List<List<Comment>> ReplyData { get; set; }
    public string ReplyCode { get; set; }
} 

工作示例 .Net fiddle here.