RestSharp를 사용하여 SendGrid의 통계 API의 응답에서 JSON을 역 직렬화하려고합니다.
요청이 성공적이고 Fiddler를 사용하여 응답이 실제로 반환되었음을 알 수 있습니다. 그러나 내가 시도 할 때
var response = client.Execute<CMSEmailTrackingStatistic>(request);
나는 null을 돌려 받는다. response.Data
다음은 요청을 발행하고 응답을받는 데 사용하는 코드입니다.
<!-- 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}]
예상 결과 목록을 만들려고했지만 작동하지 않았습니다.
나는 그들이 반환하는 Content-Type이 문제 일 수 있다고 믿기 때문에 SendGrid에 연락했습니다 (수동으로 Accept 헤더도 설정하려고했습니다).
RestSharp
JSON이 유효하지 않기 때문에 역 직렬화 할 수 없습니다.
귀하의 JSON은 시작 [
대신 {
하는 내부 포장과 동일하게 끝입니다 ]
대신}
대괄호를 제거하면 JSON을 올바르게 사용하여 역 직렬화 할 수 있습니다 Json.NET
(테스트 목적으로 만 작동하지 않아야 할 이유가 없습니다 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
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다