2
votes

I am trying to use RestSharp to deserialize JSON from a response from SendGrid's statistics API.

The request is successful and using Fiddler I can see that the response is indeed returned. However, when I try to

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

I get back null for response.Data

Here is the code I am using to issue the request and receive the response:

<!-- 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;

The class CMSEmailTrackingStatistic looks like this

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; }
}

Here is the JSON sent back from SendGrid:

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}]

I have tried making the expected result List and that didn't work.

I have contacted SendGrid because I believe the Content-Type they are returning may be an issue (I did try manually setting the Accept header as well).

1

1 Answers

2
votes

RestSharp isn't able to deserialize your JSON because it isn't valid.

Your JSON starts with a [ instead of { which is wrapped inside, and equally ends with ] instead of }

When i removed the brackets, i could deserialize you JSON properly using Json.NET (just for testing purposes, there is no reason this shouldn't work with 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 
}

Deserialized class