【发布时间】: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 标头)。
【问题讨论】: