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 with JsonConvert.DeserializeObject<>
The real error was that the response.Content
was only returning the type. You need to have...
obj = JsonConvert.DeserializeObject<List<T>>(response.Content.ReadAsStringAsync().Result);
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With