Generic Type JsonConvert.DeserializeObject<List<T>>(string)
Solution 1
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.
Solution 2
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;
}
}
Solution 3
I am not in a position where I can test this right now, but I think the following could work. I have found that JSON.NET does not like List<T>
when deserializing, I typically get around this by using arrays instead.
static HttpClient _httpClient = new HttpClient();
public static async Task<T[]> GetObject<T>(string cacheKey)
{
HttpResponseMessage response = await _httpClient .GetAsync("http://localhost:53805/api/NonPersisted/Get/" + cacheKey);
return JsonConvert.DeserializeObject<T[]>(response.Content.ToString());
}
I took the liberty of removing the using
statement for HttpClient
and made it a static member instead. I also changed the method to run async
, you can change it back easily but as I mentioned in my comment you might want to avoid HttpClient
and use WebClient instead.
Related videos on Youtube
christopher clark
Updated on July 20, 2022Comments
-
christopher clark almost 2 years
I am using Newtonsoft.JSON. I won't know the type of object passed to this method, or retrieved to this method, so I am attempting to use
DeserializeObject
on an object I do not know the type of.Is this possible? If so, how? Here is my code.
public static List<T> GetObject<T>(string cacheKey, IEnumerable<T> obj) { using (HttpClient client = new HttpClient()) { var response = client.GetAsync("http://localhost:53805/api/NonPersisted/Get/" + cacheKey).Result; obj = JsonConvert.DeserializeObject<obj.GetType>(response.Content.ToString()); return obj.ToList(); } }
I attempted first to use
obj = JsonConvert.DeserializeObject<List<T>>(response.Content.ToString());
This didn't work, obviously, it was unable to parse. Getting the Type of the object won't build, it says
obj is a variable but used like a type
.EDIT It appears you can use a generic
List<T>
without knowing the type withJsonConvert.DeserializeObject<>
The real error was that theresponse.Content
was only returning the type. You need to have...obj = JsonConvert.DeserializeObject<List<T>>(response.Content.ReadAsStringAsync().Result);
-
christopher clark almost 7 yearsI think this is going to be the easiest solution. It would have been easier for the developers, but one cast shouldn't kill them!
-
christopher clark almost 7 years
response.Content.ToString()
was the problem all along.response.Content.ReadAsStringAsync().Result
is the correct way. -
christopher clark almost 7 yearsI marked this as correct, because it appears you can use
JsonConvert.Deserialize<List<T>>
There was actually an error in my response Content. I will edit my answer to include the problem -
Tom McDonough over 4 yearsThis returns object and not what the question wants which is a List<T>