如何对列表<class>返回使用RestResponse?

时间:2018-11-08 06:17:40

标签: restsharp

首先,感谢您的所有帮助-我一直在网站上寻找答案,而他们在这里的机会很多!

现在我收到以下错误:

“无法将类型为“ System.String”的对象转换为类型为“ System.Collections.Generic.IDictionary`2 [System.String,System.Object]”。”

尝试运行以下代码时:

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

我可以在浏览器中运行该命令,并且已经验证了返回的JSON看起来正确-我什至通过在线验证工具对其进行了验证,并提供了以下信息:

[{
	"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
}]

我不知道自己缺少什么,我真的希望有人能够提供帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

所以我在另一个主题-RestSharp Deserialization of empty string to Dictionary Error

中找到了这个答案

基于此,我添加了NewtonSoft.Json,然后添加了以下代码:

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

做到了!