如何为列表 <class> return 使用 RestResponse?

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

首先,感谢您提供的所有帮助 - 我一直在网站上寻找答案,他们来这里的次数多于不!

现在我收到以下错误:

"Unable to cast object of type 'System.String' to type '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
}]

我不知道我错过了什么,我真的希望有人能提供一些帮助。

谢谢!

所以我在另一个主题中找到了这个答案 -

在此基础上,我添加了 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);

成功了!