将Json反序列化为List C#(Deserialize Json to List C#)

关于api返回的Json的反序列化,我遇到了一些问题。

Json如下:

{ "Result":[ { "Id": 1, "Type": "Type1" }, { "Id": 2, "Type": "Type2" } ]}

我正在尝试将其反序列化为此类型的列表:

public class ContactType { public int Id { get; set; } public string Type { get; set; } }

下面使用的ReturnType是:List <ContactType>,并且以这种方式调用函数GetContactTypes:

var test = await _items.GetContactTypes<List<ContactType>>(AuthToken.access_token);

使用此代码:

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); } }

但我总是得到这个错误:

无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.List`1',因为该类型需要一个JSON数组(例如[1,2,3])来反序列化正确。

感谢任何帮助,谢谢。

I'm having some issues on deserializing a Json that the api returns.

The Json is the following:

{ "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:

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 (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Any help is appreciated, Thank you.

最满意答案

您尝试反序列化的对象有一个包含ContactType数组的属性Result - 您在对象模型中没有这个。 尝试反序列化到此对象:

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; } }

接着:

... var test = await _items.GetContactTypes<MyObject>(AuthToken.access_token); ...

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. 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); ...

更多推荐