Search code examples
c#json.netjson-deserialization

Deserialize Json to List C#


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<ReturnType> 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.


Solution

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