Search code examples
c#.netjsonserializationimgur

Cannot deserialize the current JSON object error from IMGUR API in C#


I am trying to get all the images from an imgur album. It works fine with all of the albums, except when i try to get images from this album

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable`1[Imgur.API.Models.Impl.Image]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Here is my code, which throws out the exception.

public async Task<List<IImage>> dosomething()
{
    IEnumerable<IImage> image = await Task.Run(AccessTheWebAsync);
    return image.ToList(); ;
}
async Task<IEnumerable<IImage>> AccessTheWebAsync()
{
    var client = new ImgurClient(token);
    var endpoint = new AlbumEndpoint(client);
    IEnumerable<IImage> images = await endpoint.GetAlbumImagesAsync(albumlink);
    return images;
}

Solution

  • The value for albumlink that you're passing is incorrect, it should not have the #0 on the end.

    The value for albumlink should just be Dc2k6.

    I'm not sure why the #0 is there, I assume it can select a specific image or something but I couldn't get it to work. To prove it's irrelevant, add any other value after the #:

    https://i.sstatic.net/f9pQ6.jpg