Generic Type JsonConvert.DeserializeObject<List<T>>(string)

You can make GetObject method generic without having parameter IEnumerable<T> obj.

Following solution I am suggesting with assumption that you know the format of the JSON value being returned from the URL.

For example, that the URL returns JSON which contains array of items and each item has two properties firstName and lastName.

var response = "[{\"firstName\":\"Melanie\",\"lastName\":\"Acevedo\"},
    {\"firstName\":\"Rich\",\"lastName\":\"Garrett\"},
    {\"firstName\":\"Dominguez\",\"lastName\":\"Rose\"},
    {\"firstName\":\"Louisa\",\"lastName\":\"Howell\"},
    {\"firstName\":\"Stone\",\"lastName\":\"Bean\"},
    {\"firstName\":\"Karen\",\"lastName\":\"Buckley\"}]";

I can write GetObject method as following.

public static List<T> GetObject<T>()
{
    var response = "
        [{\"firstName\":\"Melanie\",\"lastName\":\"Acevedo\"},
        {\"firstName\":\"Rich\",\"lastName\":\"Garrett\"},
        {\"firstName\":\"Dominguez\",\"lastName\":\"Rose\"},
        {\"firstName\":\"Louisa\",\"lastName\":\"Howell\"},
        {\"firstName\":\"Stone\",\"lastName\":\"Bean\"},
        {\"firstName\":\"Karen\",\"lastName\":\"Buckley\"}]";

    var obj = JsonConvert.DeserializeObject<List<T>>(response);
        return obj.ToList();
}

Here T in above method can by any type which has properties firstName and lastName. For example

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
}

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double Salary { get; set; }
}

I can call GetObject method by passing either Person or Employee and get the JSON string deserialize to the collection of objects of these classes as following.

var persons = GetObject<Person>();

foreach (var item in persons)
{
    Console.WriteLine($"{item.FirstName} {item.LastName}");
}

var employees = GetObject<Employee>();

foreach (var item in employees)
{
    Console.WriteLine($"{item.FirstName} {item.LastName}");
}

Overall, the point I am trying to make is if format of the JSON is know the passing appropriate Type to JsonConvert.Deserialize<T> should work without any problem.

If incoming JSON represents a collection and trying to deserialize it to a simple class would fail and vice versa too will not work.

So for your problem, if you know that JSON is going to be a collection then using JsonConvert.Deserialize<List<T>> should not give you any problem as long T has the properties to which values from JSON can be set.

I hope this would help you resolve your issue.


I don't think you can call Deserialize<T>(..) if you do not known type T. Only thing I can think of is getting Object:

    public static Object GetObject(string cacheKey)
    {
        using (HttpClient client = new HttpClient())
        {
            var response = client.GetAsync("http://localhost:53805/api/NonPersisted/Get/" + cacheKey).Result;

            var obj = JsonConvert.DeserializeObject(response.Content.ToString());
            return obj;
        }

    }