I am getting response back in JSON format. I am using jsSerializer to get the value of the field from the JSON. It is working in most part but I am not sure why in one situation the coming back JSON has \n
in it, and for this reason, jsSerializer is not working.
This is my code:
protected void btnStats_Click(object sender, EventArgs e)
{
IRestResponse response = GetStats();//Stats provide you with the summary of the events that occur with your messages.
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
var jsobj = jsSerializer.Deserialize<dynamic>(response.Content);
int total_count= Convert.ToInt32(jsobj[0]["total_count"]); //I am getting error
and this is the method that is returning JSON:
public static IRestResponse GetStats()
{
RestClient client = new RestClient();
client.BaseUrl = "https://api.mailgun.net/v2";
client.Authenticator =
new HttpBasicAuthenticator("api",
"key");
RestRequest request = new RestRequest();
request.AddParameter("domain",
"messenger.test.com", ParameterType.UrlSegment);
request.Resource = "{domain}/stats";
request.AddParameter("event", "sent");
request.AddParameter("event", "opened");
request.AddParameter("skip", 1);
request.AddParameter("limit", 2);
return client.Execute(request);
}
I have sevaral of these methods that are working fine and only difference that I found is the JSON format that is in (response.Content
) has \n
. I haven't done anything in order to remove \n
; it just wasn't there.
This is the JSON I am getting from that code:
"{\n \"total_count\": 54,\n \"items\": [\n {\n \"total_count\": 715,\n \"created_at\": \"Mon, 04 Nov 2013 00:00:00 GMT\",\n \"tags\": {},\n \"id\": \"5276e3835b8917d8268a6df1\",\n \"event\": \"opened\"\n },\n {\n \"total_count\": 688,\n \"created_at\": \"Sun, 03 Nov 2013 00:00:00 GMT\",\n \"tags\": {},\n \"id\": \"527592035b8917d8268a1348\",\n \"event\": \"sent\"\n }\n ]\n}"
and in the other method that is working fine, I get this kind of JSON:
"[{\"unique\": {\"link\": 1}, \"total\": 35, \"recipient\": \"cutesycanvas@gmail.com\"}, {\"unique\": {\"link\": 1}, \"total\": 22, \"recipient\": \"angelina40d@hotmail.com\"}]"