Json 反序列化数组

Json Deserialize Array

我是 Json 的新手,正在尝试用它做一些例子。我有 Json 这样的数据:

{
  "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": "3,279,547",
  "Production": "Walt Disney Pictures",
  "Website": "http://marvel.com/avengers_movie",
  "Response": "True"
}

我可以获取数据并正常读取它,但是在反序列化时出现以下错误:

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.

这是我的代码

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

对于模型来说,这是第一个主要的:

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; }

第二个用于评级:

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

它是关于 Json 数组的,但我查看了有关此问题的问题并尝试修复它但没有成功。我错过了什么?

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

您的 json 字符串只有一个 Main 对象,您正试图获取一个列表

您尝试将一个 JSON 对象反序列化为对象列表。

这是一个简单对象的例子:

{ "field": 123 }

要反序列化它,您需要:

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

但是如果你有一个对象数组:

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

您将能够将它们反序列化为这样的列表:

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

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

您问题的解决方案:

  • 将反序列化类型更改为单个对象。
  • []
  • 包围你的 JSON

您正在尝试将 Main 类型的单个对象反序列化为对象列表。

您可以更改代码以反序列化为单个对象而不是列表,或者您可以更改 JSON 以表示对象数组。

第一个选项是

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

还有第二个选项

[{"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":"3,279,547","Production":"Walt Disney Pictures","Website":"http://marvel.com/avengers_movie","Response":"True"}]

(只需加括号)

您必须选择哪个选项取决于您的要求,即如果您想要允许多个对象或只允许一个对象。

当我们调用 api 'http://www.omdbapi.com/?apikey=7663ce8e&t=Avenger' 时,我们得到一个对象而不是对象数组

尝试

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

如果您想要电影列表,请尝试其他 api b.e。: themoviedbAPI