Search code examples
c#jsonjson.netdeserializationomdbapi

Deserialize with JSON


I downloaded a JSON string from OMDb API, now I want to deserialize to a list of objects but I get the error code:

Newtonsoft.Json.JsonSerializationException: "Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MovieReommendation.Movie]' 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. Path 'Search', line 1, position 10."

This is my code:

public void Import(string URL)
{
    using (WebClient wc = new WebClient())
    {
        var jsonString = wc.DownloadString(URL);
        var Json = JsonConvert.DeserializeObject<List<Movie>>(jsonString);
    }
}


public class Movie
{
    #region properties
    [JsonProperty("Title")]
    public string Title { get; set; }

    [JsonProperty("Year")]
    public string Year { get; set; }

    [JsonProperty("Type")]
    public string Type { get; set; }

    [JsonProperty("imbdID")]
    public int imbdID { get; set; }

    [JsonProperty("Poster")]
    public string Poster { get; set; }
}
{
    "Search": [{
        "Title": "Batman Begins",
        "Year": "2005",
        "imdbID": "tt0372784",
        "Type": "movie",
        "Poster": "https://m.media-amazon.com/images/M/MV5BOTY4YjI2N2MtYmFlMC00ZjcyLTg3YjEtMDQyM2ZjYzQ5YWFkXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_SX300.jpg"
    }, {
        "Title": "Batman v Superman: Dawn of Justice",
        "Year": "2016",
        "imdbID": "tt2975590",
        "Type": "movie",
        "Poster": "https://m.media-amazon.com/images/M/MV5BYThjYzcyYzItNTVjNy00NDk0LTgwMWQtYjMwNmNlNWJhMzMyXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_SX300.jpg"
    }, {....

Solution

  • You need a Root class.

    public class Root
    {
        public List<Movie> Search { get; set; }
    }
    
    public class Movie
    {
        [JsonProperty("Title")]
        public string Title { get; set; }
    
        [JsonProperty("Year")]
        public string Year { get; set; }
    
        [JsonProperty("Type")]
        public string Type { get; set; }
    
        [JsonProperty("imbdID")]
        public int imbdID { get; set; }
    
        [JsonProperty("Poster")]
        public string Poster { get; set; }
    }
    

    And you need to deserialize the JSON to Root object.

    public void Import(string URL)
    {
        using (WebClient wc = new WebClient())
        {
            var jsonString = wc.DownloadString(URL);
            var json = JsonConvert.DeserializeObject<Root>(jsonString);
            List<Movie> movies = json.Search;
        }
    }
    

    FYI, you can generate the classes based on JSON result with json2csharp.