在具有“。”的键上使用DeserializeAsAttribute。在里面

时间:2015-06-24 21:20:49

标签: c# json restsharp

使用RestSharp的DeserializeAsAttribute时,我无法获得其中包含.的键的值。

这是我的JSON:

{
    ".issued": "Wed, 24 Jun 2015 20:59:57 GMT",
    ".expires": "Wed, 08 Jul 2015 20:59:57 GMT"
}

模型我正在反序列化:

class TokenModel
{
    [DeserializeAs(Name = ".issued")]
    public DateTime Issued { get; set; }
    [DeserializeAs(Name = ".expires")]
    public DateTime Expires { get; set; }
}

调试:

GetToken() - success - issued: 1/1/0001 12:00:00 AM
GetToken() - success - expires: 1/1/0001 12:00:00 AM

我也将RestRequest.DateFormat设置为:

request.DateFormat = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";

我尽可能地遵循https://github.com/restsharp/RestSharp/wiki/Deserialization文档而没有任何运气。

1 个答案:

答案 0 :(得分:1)

我也想弄清楚这一点。我不得不手动解析响应内容......

    Regex IssuedRegex = new Regex(@"\""\.issued\"":\s*\""([^\""]+)\""");
    Regex ExpiresRegex = new Regex(@"\""\.expires\"":\s*\""([^\""]+)\""");

    var response = authClient.Execute<AccessToken>(request);
    AccessToken = response.Data;

    // workaround for bug in RestSharp
    var im = IssuedRegex.Match(response.Content);
    if (im.Success)
        AccessToken.Issued = DateTime.ParseExact(im.Groups[1].Value, "R", CultureInfo.InvariantCulture);

    var em = ExpiresRegex.Match(response.Content);
    if (em.Success)
        AccessToken.Expires = DateTime.ParseExact(em.Groups[1].Value, "R", CultureInfo.InvariantCulture);