0

I am new to Json and trying to do some examples with it. I have Json data like this:

{
  "Title": "The Avengers",
  "Year": "2012",
  "Rated": "PG-13",
  "Released": "04 May 2012",
  "Runtime": "143 min",
  "Genre": "Action, Adventure, Sci-Fi",
  "Director": "Joss Whedon",
  "Writer": "Joss Whedon (screenplay), Zak Penn (story), Joss Whedon (story)",
  "Actors": "Robert Downey Jr., Chris Evans, Mark Ruffalo, Chris Hemsworth",
  "Plot": "Earth's mightiest heroes must come together and learn to fight as a team if they are going to stop the mischievous Loki and his alien army from enslaving humanity.",
  "Language": "English, Russian, Hindi",
  "Country": "USA",
  "Awards": "Nominated for 1 Oscar. Another 38 wins & 79 nominations.",
  "Poster": "https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg",
  "Ratings": [
    {
      "Source": "Internet Movie Database",
      "Value": "8.0/10"
    },
    {
      "Source": "Rotten Tomatoes",
      "Value": "92%"
    },
    {
      "Source": "Metacritic",
      "Value": "69/100"
    }
  ],
  "Metascore": "69",
  "imdbRating": "8.0",
  "imdbVotes": "1,200,683",
  "imdbID": "tt0848228",
  "Type": "movie",
  "DVD": "25 Sep 2012",
  "BoxOffice": "$623,279,547",
  "Production": "Walt Disney Pictures",
  "Website": "http://marvel.com/avengers_movie",
  "Response": "True"
}

I can get the data and read it just fine but when it comes deserialize I get the following error:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Deneme.Modeller.Main]' 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.

This is my code

string url = "http://www.omdbapi.com/?apikey=7663ce8e&t=Avengers";
WebRequest request = WebRequest.Create(url);
WebResponse reply;
reply = request.GetResponse();
StreamReader returninfo = new StreamReader(reply.GetResponseStream());
string getinfo = returninfo.ReadToEnd();
List<Main> Info = JsonConvert.DeserializeObject<List<Main>>(getinfo);

and for models this is first main:

public string Title { get; set; }
public string Year { get; set; }
public string Rated { get; set; }
public string Released { get; set; }
public string Runtime { get; set; }
public string Genre { get; set; }
public string Director { get; set; }
public string Writer { get; set; }
public string Actors { get; set; }
public string Plot { get; set; }
public string Language { get; set; }
public string Country { get; set; }
public string Awards { get; set; }
public string Poster { get; set; }
public List<Rating> Ratings { get; set; }
public string Metascore { get; set; }
public string imdbRating { get; set; }
public string imdbVotes { get; set; }
public string imdbID { get; set; }
public string Type { get; set; }
public string DVD { get; set; }
public string BoxOffice { get; set; }
public string Production { get; set; }
public string Website { get; set; }
public string Response { get; set; }

second one is for Ratings:

public string Source { get; set; }
public string Value { get; set; }
public virtual ICollection<Main> Mains { get; set; }

It's about Json array, but I looked asked questions about this problem and tried to fix it but no luck. What am I missing?

2
  • A Json array will always start with a square bracket [, a Json object will always start with a curly bracket {. An object != array Commented Oct 4, 2019 at 8:33
  • @Selvin LOL thats right. I was like wtf when I saw the comments damn blindness. Thanks for the answers. Commented Oct 4, 2019 at 8:41

4 Answers 4

1

You are trying to deserialize a single object of type Main into a list of objects.

You can either change your code to deserialize into a single object instead of a list or you can alter your JSON to represent an array of objects.

The first option would be

Main Info = JsonConvert.DeserializeObject<Main>(getinfo);

And the second option

[{"Title":"The Avengers","Year":"2012","Rated":"PG-13","Released":"04 May 2012","Runtime":"143 min","Genre":"Action, Adventure, Sci-Fi","Director":"Joss Whedon","Writer":"Joss Whedon (screenplay), Zak Penn (story), Joss Whedon (story)","Actors":"Robert Downey Jr., Chris Evans, Mark Ruffalo, Chris Hemsworth","Plot":"Earth's mightiest heroes must come together and learn to fight as a team if they are going to stop the mischievous Loki and his alien army from enslaving humanity.","Language":"English, Russian, Hindi","Country":"USA","Awards":"Nominated for 1 Oscar. Another 38 wins & 79 nominations.","Poster":"https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"8.0/10"},{"Source":"Rotten Tomatoes","Value":"92%"},{"Source":"Metacritic","Value":"69/100"}],"Metascore":"69","imdbRating":"8.0","imdbVotes":"1,200,683","imdbID":"tt0848228","Type":"movie","DVD":"25 Sep 2012","BoxOffice":"$623,279,547","Production":"Walt Disney Pictures","Website":"http://marvel.com/avengers_movie","Response":"True"}]

(simply add brackets)

Which option you have to choose is depending on your requirements, i.e. if you want to allow multiple objects or just one.

Sign up to request clarification or add additional context in comments.

Comments

0
Main Info = JsonConvert.DeserializeObject<Main>(getinfo);

Your json string has only one Main object, you were trying to get a List

Comments

0

You try to deserialize one JSON object into a list of objects.

This is an example of simple object:

{ "field": 123 }

To deserialize it you need to:

var obj = JsonConvert.DeserializeObject<SomeModel>(json);

But if you have an array of objects:

[{ "field": 123 }, { "field": 123 }]

You will be able to deserialize them to a list like this:

var objs = JsonConvert.DeserializeObject<SomeModel[]>(json);

or

var objs = JsonConvert.DeserializeObject<List<SomeModel>>(json);

Solutions to your question:

  • Change deserialization type to a single object.
  • Wrap your JSON around with []

Comments

0

when we call api 'http://www.omdbapi.com/?apikey=7663ce8e&t=Avenger' we gets an objet not an array of object

try

 var info = JsonConvert.DeserializeObject<Main>(getinfo);

If you want list of movies try a other api b.e.: themoviedbAPI

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.