通用类型JsonConvert.DeserializeObject< List< T>>(字符串) [英] Generic Type JsonConvert.DeserializeObject<List<T>>(string)
问题描述
我正在使用Newtonsoft.JSON.我不知道传递给此方法或检索到该方法的对象的类型,所以我尝试在不知道其类型的对象上使用DeserializeObject
.
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();
}
}
我首先尝试使用
obj = JsonConvert.DeserializeObject<List<T>>(response.Content.ToString());
这显然行不通,无法解析.
obj is a variable but used like a type
.
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
.
编辑
似乎可以使用通用List<T>
而不用JsonConvert.DeserializeObject<>
知道类型.真正的错误是response.Content
仅返回类型.你需要...
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);
推荐答案
您可以使GetObject
方法通用,而无需使用参数IEnumerable<T> obj
.
You can make GetObject
method generic without having parameter IEnumerable<T> obj
.
以下解决方案是建议您假设您知道从URL返回的JSON值的格式.
Following solution I am suggesting with assumption that you know the format of the JSON value being returned from the URL.
例如,URL返回包含项目数组的JSON,并且每个项目都有两个属性firstName
和lastName
.
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\"}]";
我可以编写如下的GetObject
方法.
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();
}
在上述方法中的T
可以具有属性firstName
和lastName
的任何类型.例如
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; }
}
我可以通过传递Person
或Employee
来调用GetObject
方法,如下所示将JSON字符串反序列化到这些类的对象集合中.
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}");
}
总的来说,我要说的是,如果知道JSON格式,则可以将适当的Type传递给JsonConvert.Deserialize<T>
应该可以正常工作.
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.
如果传入的JSON表示一个集合,并且尝试将其反序列化为一个简单的类将失败,反之亦然.
If incoming JSON represents a collection and trying to deserialize it to a simple class would fail and vice versa too will not work.
所以对于您的问题,如果您知道JSON将成为一个集合,那么使用JsonConvert.Deserialize<List<T>>
应该不会给您带来任何问题,只要T
具有可以设置JSON值的属性即可.
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.
这篇关于通用类型JsonConvert.DeserializeObject< List< T>>(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!