【问题标题】:Why does RestSharp throw an error when deserializing a boolean response?为什么 RestSharp 在反序列化布尔响应时会抛出错误?
【发布时间】:2013-04-01 15:14:05
【问题描述】:

当我像这样在 RestSharp 中提出请求时:

var response = client.Execute<bool>(request);

我收到以下错误:

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

这是完整的 HTTP 响应,每个 Fiddler:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 01 Apr 2013 15:09:14 GMT
Content-Length: 5

false

似乎所有的反应都是犹太教的,所以什么给出了?

另外,如果我通过返回一个简单的值而不是一个对象来使用我的 WebAPI 控制器做一些愚蠢的事情并且可以解决我的问题,请随时提出建议。

【问题讨论】:

    标签: restsharp


    【解决方案1】:

    RestSharp 只会反序列化有效的 json。 false 不是有效的 json(根据 RFC-4627)。服务器至少需要返回如下内容:

    { "foo": false }
    

    你需要一个像下面这样的类来反序列化:

    public class BooleanResponse
    {
        public bool Foo { get; set; }
    }
    

    【讨论】:

    猜你喜欢
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多