Sese1997
Sese1997

Reputation: 13

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"
    }, {....

Upvotes: 1

Views: 249

Answers (3)

njari
njari

Reputation: 192

Your json is not an array. It's an object with a key that is an array. There is a difference between these two classes. Your json represents the first one, not the second. The second one is what you tried to serialise it into.

public class Search {
List<Movie> search ; 
}
List<Movie> movies;

Upvotes: 0

Yong Shun
Yong Shun

Reputation: 51485

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.

Upvotes: 1

Karen Payne
Karen Payne

Reputation: 5157

Try defining a class as follows

  • Copy the json to the windows clipboard
  • Create a new class in your project
  • From Visual Studio edit menu select paste special, Paste Json as class into the class just created
  • Remove the original class
  • In the example below, MoviesRoot was RootObject

Now when DeserializeObject, type it to MoviesRoot and you should be good to go

public class MoviesRoot
{
    public Search[] Search { get; set; }
}

public class Search
{
    public string Title { get; set; }
    public string Year { get; set; }
    public string imdbID { get; set; }
    public string Type { get; set; }
    public string Poster { get; set; }
}

Note I noticed another reply as posting so the main differences is between converting json inside of Visual Studio or using a third party tool.

Upvotes: 0

Related Questions