0

I am trying to parse a List of Comment objects from here. A comment object is a class in the leankit namespace: LeanKit.API.Client.Library.TransferObjects.Comment but I get an error in the final line of the block below specifically over responseString:

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

Why am I getting this? I am specifying a custom class I created specifically to deserialize the list:

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

calling the 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);

a cleaner version of calling the class (using Flurl):

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

The JSON structure (from the link above) reproduced here:

{
  "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."
}
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • You need to include what `Comment` is – maccettura May 02 '18 at 21:29
  • `ReplyCode` should also be an `int`, not a `string`. – Ron Beyer May 02 '18 at 21:33
  • 2
    In the JSON, `"ReplyData"` is a 2d jagged array but you model it as a 1d list: `public List ReplyData { get; set; }`. Change it to `public List> ReplyData { get; set; }` and it should work. – dbc May 02 '18 at 21:34
  • @maccettura I added it to the first sentence, not sure how to be more specific. What other info would be helpful? – Rilcon42 May 02 '18 at 21:39
  • @dbc Just tried that, but it didnt help. I also specified where the error was occuring if it helps – Rilcon42 May 02 '18 at 21:39
  • @Rilcon42, Yet it does work since I've tried it myself as well :) (Although I've simplified the Comment class as well, keeping just the first 2 properties) – Frederik Gheysels May 02 '18 at 21:41
  • would one of you mind posting that as a solution please? If it works for you, clearly Im missing something – Rilcon42 May 02 '18 at 21:41
  • @Rilcon42 - `public List> ReplyData` works for me, see https://dotnetfiddle.net/w3Z8PT. Is this your `Comment` class? https://github.com/LeanKit/LeanKit.API.Client/blob/master/LeanKit.API.Client.Library/TransferObjects/Comment.cs – dbc May 02 '18 at 21:44
  • 1
    @Rilcon42 - If https://dotnetfiddle.net/w3Z8PT does not work, please [edit] your question to include the full `ToString()` output of the exception including the exception type, message, traceback and inner exception, if any. – dbc May 02 '18 at 21:46
  • @dbc your code works perfectly. No clue how you pulled that together so fast. Im going to read through it and make sure my code follows yours. In the mean time please propose that as an answer – Rilcon42 May 02 '18 at 21:47
  • @Rilcon42, `[[` is a pretty clear sign of `Collection of Collection`, Collection can be any collection type List, Array etc' – Orel Eraki May 02 '18 at 21:49
  • @Rilcon42 - I edited your question to add what I believe are the correct links to leankit and Comment.cs. If I found the wrong links, please let me know. – dbc May 02 '18 at 22:00

1 Answers1

3

In the JSON, "ReplyData" is a 2d jagged array:

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

In you model it is a 1d list:

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

You need to change it to public List<List<Comment>> ReplyData { get; set; } to reflect the actual JSON:

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

Where I assume Comment is taken from https://github.com/LeanKit/LeanKit.API.Client/blob/master/LeanKit.API.Client.Library/TransferObjects/Comment.cs.

If there is a chance that it is sometimes a 1d array and sometimes a 2d array, you may need to apply SingleOrArrayConverter<Comment> from this answer to How to handle both a single item and an array for the same property using JSON.net by Brian Rogers like so:

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

Working sample .Net fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340