I have the following JSON object being returned from an API call beyond my control:
{
"GetBusinessGroupsRestResult": [
{
"BgId": 1,
"NodeLevel": 1,
"BusinessDescription": "Business Groups",
"BusinessName": "Root",
"nodegroupid": 2,
"nodegroupname": "Root",
"nodegroupdesc": "Root",
"sorkkey": "Root",
"Marked": 2
},
{
"BgId": 2,
"NodeLevel": 2,
"BusinessDescription": "Business A",
"BusinessName": "Business A",
"ParentID": 1,
"nodegroupid": 3,
"nodegroupname": "Business A",
"nodegroupdesc": "Business A",
"sorkkey": "Business A",
"Marked": 2
},
...
]
}
I have created the following class structure in an attempt to deserialise it into C# object instances:
public class GetBusinessGroupsRestResult
{
public List<BusinessGroup> BusinessGroups { get; set; }
}
public class BusinessGroup
{
[DeserializeAs(Name = "BgId")]
public int BusinessGroupId { get; set; }
public int NodeLevel { get; set; }
public string BusinessDescription { get; set; }
public string BusinessName { get; set; }
[DeserializeAs(Name = "ParentID")]
public int ParentId { get; set; }
[DeserializeAs(Name = "nodegroupid")]
public int NodeGroupId { get; set; }
[DeserializeAs(Name = "nodegroupname")]
public string NodeGroupName { get; set; }
[DeserializeAs(Name = "sorkkey")]
public string SortKey { get; set; }
public int Marked { get; set; }
}
I am trying to deserialise it using RestSharp, specifying the RootElement as GetBusinessGroupsRestResult. When attempting the deserialisation using RestSharp's IRestResponse<T> Execute<T>(RestRequest), I recieve the following error:
System.InvalidCastException: Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.
I've managed to deserialise responses from this API that don't return lists before, for example:
{
"AcquireSecurityTokenRestResult": {
"SecurityToken": "SECURITY-TOKEN",
"SessionLength": 600000,
"APIVersion": "VERSION"
}
}
With the following POCO:
public class AcquireSecurityTokenRestResult
{
public string SecurityToken { get; set; }
public int SessionLength { get; set; }
[DeserializeAs(Name = "APIVersion")]
public string ApiVersion { get; set; }
}
Can anyone point me in the right direction here? Thank you!