[英]Deserialize Json to List C#
I'm having some issues on deserializing a Json that the api returns. 我在反序列化api返回的Json时遇到一些问题。
The Json is the following: Json是以下内容:
{ "Result":[
{
"Id": 1,
"Type": "Type1"
},
{
"Id": 2,
"Type": "Type2"
}
]}
I'm trying to deserialize it to a list of this type: 我正在尝试将其反序列化为这种类型的列表:
public class ContactType
{
public int Id { get; set; }
public string Type { get; set; }
}
The ReturnType used below is: List< ContactType > and the function GetContactTypes is called this way: 下面使用的ReturnType是:List <ContactType>,并以这种方式调用函数GetContactTypes:
var test = await _items.GetContactTypes<List<ContactType>>(AuthToken.access_token);
Using this code: 使用此代码:
public async Task<ReturnType> GetContactTypes<ReturnType>(string access_token)
{
try
{
Header = string.Format("Bearer {0}", access_token);
client.DefaultRequestHeaders.Add("Authorization", Header);
HttpResponseMessage response = await client.GetAsync(base_url + "contacttypes");
if (response.StatusCode == HttpStatusCode.OK)
{
return JsonConvert.DeserializeObject<ReturnType>(await response.Content.ReadAsStringAsync());
}
else
{
return default(ReturnType);
}
}
catch (Exception ex)
{
return default(ReturnType);
}
}
But I always get this error: 但是我总是会收到这个错误:
Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1',因为该类型需要JSON数组(例如[1,2,3])来反序列化正确地。
Any help is appreciated, Thank you. 任何帮助表示赞赏,谢谢。
The object you're trying to deserialize has a single property Result
containing the array of ContactType
- you dont have that in your object model. 您要反序列化的对象具有单个属性
Result
其中包含ContactType
数组-您的对象模型中没有该属性。 Try deserializing to this object: 尝试反序列化此对象:
public class MyClass // give this a more meaningful name
{
public List<ContactType> Result{ get; set; }
}
public class ContactType
{
public int Id { get; set; }
public string Type { get; set; }
}
and then: 接着:
...
var test = await _items.GetContactTypes<MyObject>(AuthToken.access_token);
...
You probably need a wrapper class for this, since the json object is not just an array itself but with a node "Result"
with the value of that array. 您可能需要一个包装器类,因为json对象不仅是数组本身,而且带有带有该数组值的节点
"Result"
。
public class ContactResult
{
public List<ContactType> Result {get; set;}
}
And then call your method with that wrapper class: 然后使用该包装器类调用您的方法:
var test = await _items.GetContactTypes<ContactResult>(AuthToken.access_token);
If you generate the C# classes for this JSON at json2csharp.com you will see that it creates two classes: ContactType and RootObject: 如果您在json2csharp.com上为此JSON生成C#类,则会看到它创建了两个类:ContactType和RootObject:
public class ContactType
{
public int Id { get; set; }
public string Type { get; set; }
}
public class RootObject
{
public List<ContactType> Result { get; set; }
}
Your code is failing because it is trying to deserialize the JSON to type List<ContactType>
when it is actually of type RootObject with a containing list. 您的代码失败,因为当它实际上是带有包含列表的RootObject类型时,它试图反序列化JSON以键入
List<ContactType>
。
So if you deserialize to type of RootObject
first like so: 因此,如果您首先反序列化
RootObject
类型, RootObject
所示:
JsonConvert.DeserializeObject<RootObject>(str)
You can then access the list inside of the deserialized RootObject
object. 然后,您可以访问反序列化
RootObject
对象内部的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.