【问题标题】:RestSharp Deserialization IssueRestSharp反序列化问题
【发布时间】:2014-08-14 17:25:02
【问题描述】:

我正在尝试使用 RestSharp 从 SendGrid 的统计 API 的响应中反序列化 JSON。

请求成功,使用 Fiddler 可以看到确实返回了响应。但是,当我尝试

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

response.Data 的返回值为 null

这是我用来发出请求和接收响应的代码:

<!-- language: lang-cs -->
string trackingCategory = "E-News members 08-12-14";
string url = "/stats.get.json";
var client = new RestClient(ConfigurationManager.AppSettings["SendgridAPIUrl"]);
var request = new RestRequest(url, Method.POST);
request.AddParameter("category", trackingCategory, ParameterType.GetOrPost); // adds to POST or URL querystring based on Method
request.AddParameter("api_key", ConfigurationManager.AppSettings["SendGridPassword"], ParameterType.GetOrPost); // replaces matching token in request.Resource
request.AddParameter("api_user", ConfigurationManager.AppSettings["SendGridUserName"], ParameterType.GetOrPost);
request.AddParameter("aggregate", "1", ParameterType.GetOrPost);
var response = client.Execute<CMSEmailTrackingStatistic>(request);
return response.Data;

CMSEmailTrackingStatistic 类如下所示

public class CMSEmailTrackingStatistic
{
    public int delivered { get; set; }
    public int unsubscribes { get; set; }
    public string name { get; set; }
    public int invalid_email { get; set; }
    public int bounces { get; set; }
    public int repeat_unsubscribes { get; set; }
    public int unique_clicks { get; set; }
    public int spam_drop { get; set; }
    public int repeat_bounces { get; set; }
    public int repeat_spamreports { get; set; }
    public int blocked { get; set; }
    public int requests { get; set; }
    public int spamreports { get; set; }
    public int clicks { get; set; }
    public int opens { get; set; }
    public int unique_opens { get; set; }
}

这是从 SendGrid 发回的 JSON:

HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Thu, 14 Aug 2014 17:05:30 GMT
Content-Type: text/html
Connection: keep-alive
Access-Control-Allow-Origin: https://sendgrid.com
Content-Length: 328

[{"delivered": 7153, "unsubscribes": 0, "name": "E-News nonmembers 08-12-14", "invalid_email": 30, "bounces": 13, "repeat_unsubscribes": 0, "unique_clicks": 86, "spam_drop": 0, "repeat_bounces": 412, "repeat_spamreports": 31, "blocked": 14, "requests": 7665, "spamreports": 0, "clicks": 153, "opens": 1384, "unique_opens": 967}]

我已尝试制作预期结果列表,但没有成功。

我已经联系了 SendGrid,因为我认为他们返回的 Content-Type 可能是一个问题(我也尝试过手动设置 Accept 标头)。

【问题讨论】:

    标签: c# json restsharp


    【解决方案1】:

    RestSharp 无法反序列化您的 JSON,因为它无效。

    您的 JSON 以 [ 开头,而不是包裹在里面的 {,同样以 ] 结尾,而不是 }

    当我删除括号时,我可以使用 Json.NET 正确反序列化 JSON(仅出于测试目的,没有理由这不适用于 RestSharp):

    {
      "delivered": 7153, "unsubscribes": 0, "name": "E-News nonmembers 08-12-14", 
      "invalid_email": 30, "bounces": 13, "repeat_unsubscribes": 0, "unique_clicks": 86, 
      "spam_drop": 0, "repeat_bounces": 412, "repeat_spamreports": 31, "blocked": 14, 
      "requests": 7665, "spamreports": 0, "clicks": 153, "opens": 1384, "unique_opens": 967 
    }
    

    【讨论】:

    • 我正在从返回的 JSON 中删除括号,但是,我也将其转发给了 SendGrid,希望他们能够了解我的请求/他们的结果可能出现的问题。如果我在一天左右没有听到任何消息,我会接受这个答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多