0

I'm trying to use RestSharp to deserialize json data that doesn't include property names. The order and type of the data returned matches a pre-defined order based on the request parameters.

Here's what the response data looks like:

{
    "pos": 0,
    "start": 0,
    "totalRecords": 2,
    "data": [
        [
            "bdb4b591-482e-43a5-86c8-00c9e4b913df",
            null,
            null,
            "SOUTH LAWN",
            0,
            300,
            "fffde79d-68b2-4825-aaac-9be4d03e7d13",
            true,
            "2014-07-01T17:33:37.727",
            1
        ],
        [
            "4980578e-5663-44c3-aed3-0151b085dc11",
            "125",
            "POLICE ACADEMY #",
            "125",
            634,
            32,
            "f6439406-6c74-455d-9d70-7540b514c651",
            true,
            "2014-12-04T14:50:37.707",
            3
        ]
]}

Here's the DTO class that represents the data items:

public class RoomEntity
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string RoomNumber { get; set; }
    public float SquareFootage { get; set; }
    public int MaxOccupancy { get; set; }
    public string BuildingId { get; set; }
    public bool IsActive { get; set; }
    public DateTime ModifiedDate { get; set; }
    public int RowVersion { get; set; }
}
    public class QueryResponse<TEntity>
{
    public int Pos { get; set; }
    public int Start { get; set; }
    public int TotalRecords { get; set; }
    public List<TEntity> Data { get; set; }
}

Here's my client execute:

var client = new RestClient() { BaseUrl = new Uri(BaseUrl), Timeout = Timeout };
var response = client.Execute<QueryResponse<RoomEntity>>(request);

I get this error from restsharp:

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

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
brussell
  • 69
  • 1
  • 7
  • You could enable use of Json.NET for RestSharp as described [here](https://github.com/restsharp/RestSharp/blob/master/readme.txt) then use `ObjectToArrayConverter` from [C# JSON.NET - Deserialize response that uses an unusual data structure.](https://stackoverflow.com/a/39462464/3744182). – dbc Jul 07 '17 at 17:48
  • Just a sidenote: "json without any property names" -> It has property names. What you are looking at is a json document that contains arrays. – Peter Bons Jul 07 '17 at 17:59

0 Answers0