How Do I Use a RestResponse for a List<class> return?

First, thank you for all of your help - I am constantly on the site looking for answers and they are here more times than not!

Right now I am receiving the following error:

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

When attempting to run the following code:

            var clientX = new RestClient(_context.CloudUrl + ":" + _context.CloudPort.ToString());
            clientX.AddHandler("application/json", new JsonDeserializer());
            var requestX = new RestRequest("/api/web/GetAllDevicePolicies", Method.GET, DataFormat.Json);

            IRestResponse<List<DevicePolicy>> devicePolicies = clientX.Execute<List<DevicePolicy>>(requestX);

I can run the command in a browser and I have verified that the returned JSON looks correct - I even validated it through an online validation tool and it provides this:

[{
	"id": "5be3c6802a78549350d297b0",
	"policyID": 1,
	"policyName": "CloudServer",
	"policyDesc": "Controls how cloud server agents operate",
	"policyVersion": "1.0.0.0",
	"policyAuth": "Diffie-Helman",
	"deployedLocations": null,
	"alertResp": 0,
	"policyStatus": 2
}, {
	"id": "5be3c7508b829e9abc90d4a6",
	"policyID": 2,
	"policyName": "WindowsServer",
	"policyDesc": "Controls how Windows server agents operate",
	"policyVersion": "1.0.0.0",
	"policyAuth": "Diffie-Helman",
	"deployedLocations": null,
	"alertResp": 0,
	"policyStatus": 2
}, {
	"id": "5be3c7728b829e9abc90d4a7",
	"policyID": 3,
	"policyName": "LinuxServer",
	"policyDesc": "Controls how Linux server agents operate",
	"policyVersion": "1.0.0.0",
	"policyAuth": "Diffie-Helman",
	"deployedLocations": null,
	"alertResp": 1,
	"policyStatus": 2
}, {
	"id": "5be3c79b8b829e9abc90d4a8",
	"policyID": 4,
	"policyName": "IoTDevice",
	"policyDesc": "Controls how Linux server agents operate",
	"policyVersion": "1.0.0.0",
	"policyAuth": "Diffie-Helman",
	"deployedLocations": null,
	"alertResp": 2,
	"policyStatus": 2
}, {
	"id": "5be3c7b08b829e9abc90d4a9",
	"policyID": 5,
	"policyName": "Windows Laptop",
	"policyDesc": "Controls how Linux server agents operate",
	"policyVersion": "1.0.0.0",
	"policyAuth": "Diffie-Helman",
	"deployedLocations": null,
	"alertResp": 0,
	"policyStatus": 2
}, {
	"id": "5be3c7cd8b829e9abc90d4aa",
	"policyID": 6,
	"policyName": "Android Phone",
	"policyDesc": "Controls how Android Phone agents operate",
	"policyVersion": "1.0.0.0",
	"policyAuth": "Diffie-Helman",
	"deployedLocations": null,
	"alertResp": 0,
	"policyStatus": 2
}, {
	"id": "5be3c7f28b829e9abc90d4ab",
	"policyID": 7,
	"policyName": "Linux Laptop",
	"policyDesc": "Controls how Linux Laptop agents operate",
	"policyVersion": "1.0.0.0",
	"policyAuth": "Diffie-Helman",
	"deployedLocations": null,
	"alertResp": 1,
	"policyStatus": 2
}, {
	"id": "5be3c80a8b829e9abc90d4ac",
	"policyID": 8,
	"policyName": "Windows Phone",
	"policyDesc": "Controls how Windows Phone agents operate",
	"policyVersion": "1.0.0.0",
	"policyAuth": "Diffie-Helman",
	"deployedLocations": null,
	"alertResp": 1,
	"policyStatus": 2
}]

I have no idea what I am missing and I was really hoping somebody could provide some help.

Thank you!

1 answer

  • answered 2018-11-08 07:21 Ken Tola

    So I found this answer in another topic - RestSharp Deserialization of empty string to Dictionary Error

    Based on that, I added NewtonSoft.Json and then this code:

    var clientX = new RestClient(_context.CloudUrl + ":" + _context.CloudPort.ToString());
                clientX.AddHandler("application/json", new JsonDeserializer());
                var requestX = new RestRequest("/api/web/GetAllDevicePolicies", Method.GET, DataFormat.Json);
    
                var response = clientX.Execute<DevicePolicy>(requestX);
                List<DevicePolicy> devicePolicies = JsonConvert.DeserializeObject<List<DevicePolicy>>(response.Content);
    

    That did the trick!