13

I launch this RestSharp query in JSON format:

var response = restClient.Execute<Report>(request);

The response I get contains this data

[
    {
        "Columns":
        [
            {"Name":"CameraGuid","Type":"Guid"},
            {"Name":"ArchiveSourceGuid","Type":"Guid"},
            {"Name":"StartTime","Type":"DateTime"},
            {"Name":"EndTime","Type":"DateTime"},
            {"Name":"TimeZone","Type":"String"},
            {"Name":"Capabilities","Type":"UInt32"}
        ],
        "Rows":
        [
            [
                "00000001-0000-babe-0000-00408c71be50",
                "3782fe37-6748-4d36-b258-49ed6a79cd6d",
                "2013-11-27T17:52:00Z",
                "2013-11-27T18:20:55.063Z",
                "Eastern Standard Time",
                2147483647
            ]
        ]
    }
]

I'm trying to deserialize it into this group of classes:

public class Report
{
    public List<ReportResult> Results { get; set; }
}

public class ReportResult
{
    public List<ColumnField> Columns { get; set; }
    public List<RowResult>   Rows { get; set; }
}

public class ColumnField
{
    public string Name { get; set; }
    public string Type { get; set; }
}

public class RowResult
{
    public List<string> Elements { get; set; }
}

Unfortunately, the result data is null and I get this exception:

Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.

I cannot figure out what is wrong here. I little help would be greatly appreciated.

2 Answers 2

23

Try this:

var response = restClient.Execute<List<ReportResult>>(request);

EDIT

You should also change ReportResult to:

public class ReportResult
{
  public List<ColumnField> Columns { get; set; }
  public List<List<string>>   Rows { get; set; }
}

and you can get rid of Report and RowResult.

Sign up to request clarification or add additional context in comments.

6 Comments

I think he'll also have a problem due to the Elements property. Rows should be a List<string> (unless you can tell JSON.net that's how it is, maybe via attributes).
Good catch, Rows should be List<List<string>> though
Using IList<ReportResult> gives my a compilation error: Error CS0310: The type System.Collections.Generic.IList<ReportResult>' must have a public parameterless constructor in order to use it as parameter T' in the generic type or method `ExecuteRequest<T>(RestSharp.RestRequest)' (CS0310)
Change it to List<ReportResult>, I edited the answer accordingly
Also, trying the List<List<string>> gives my the same error (unable to cast...)
|
0

There is another way by creating wrapper class:

public class ThirdPartySuggesters : List<ThirdPartySuggester> {}
var response = client.Execute<ThirdPartySuggesters>(request);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.