list tickets and include requester - json problem

Hello,

I'm using a example from Github (https://github.com/freshdesk/fresh-samples/blob/master/C-Sharp/GetTickets.cs) and I have a little bit of drouble with the responseBody.

I added some new lines to deserialize the responseBody to a class "ticket". Also I changed the apiPath to "/api/v2/tickets?include=requester", cause I wanted to get the requester too.   

using System;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace ConsoleSample
{
    class Program
    {
        static void Main(string[] args)
        {
            string fdDomain = "YOUR_DOMAIN"; // your freshdesk domain
            string apiKey = "YOUR_API_KEY";
            string apiPath = "/api/v2/tickets?include=requester"; // API path
            string responseBody = String.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://" + fdDomain + ".freshdesk.com" + apiPath);
            request.ContentType = "application/json";
            request.Method = "GET";
            string authInfo = apiKey + ":X"; // It could be your username:password also.
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            request.Headers["Authorization"] = "Basic " + authInfo;
            try
            {
                Console.WriteLine("Submitting Request");
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Stream dataStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(dataStream);
                    responseBody = reader.ReadToEnd();
                    reader.Close();
                    dataStream.Close();
                    //return status code
                    Console.WriteLine("Status Code: {1} {0}", ((HttpWebResponse)response).StatusCode, (int)((HttpWebResponse)response).StatusCode);
                }
                Console.Out.WriteLine(responseBody);
            }
            catch (WebException ex)
            {
                Console.WriteLine("API Error: Your request is not successful. If you are not able to debug this error properly, mail us at support@freshdesk.com with the follwing X-Request-Id");
                Console.WriteLine("X-Request-Id: {0}", ex.Response.Headers["X-Request-Id"]);
                Console.WriteLine("Error Status Code : {1} {0}", ((HttpWebResponse)ex.Response).StatusCode, (int)((HttpWebResponse)ex.Response).StatusCode);
                using (var stream = ex.Response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    Console.Write("Error Response: ");
                    Console.WriteLine(reader.ReadToEnd());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(ex.Message);
            }

            //NEW LINES

            try
            {
                List<Ticket> tickets = JsonConvert.DeserializeObject<List<Ticket>>(responseBody);
            }
            catch(Exception ex)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }

        public class Ticket
        {
            public ulong id { get; set; }
            public List<string> cc_emails { get; set; }
            public string company_id { get; set; }
            public string description_text { get; set; }
            public DateTime created_at { get; set; }
            public List<Requester> requester { get; set; }
        }

        public class Requester
        {
            public ulong id { get; set; }
            public string name { get; set; }
            public string email { get; set; }
        }
    }
}

  

But I get always following exception.

Exception.Message
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ConsoleSample.Program+Requester]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '[0].requester.id', line 1, position 1732.


Is there maybe something wrong in the responseBody? Did I made a error?


thanks
phillip

Login or Signup to post a comment