Json反序列化数组

时间:2019-10-04 08:27:34

标签: c# json json-deserialization

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

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

  

Newtonsoft.Json.JsonSerializationException:'无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [Deneme.Modeller.Main]',因为类型需要JSON数组(例如[1,2,3])才能正确反序列化。   要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型数组或列表),可以从JSON对象反序列化。还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。

这是我的代码

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数组的,但是我查看了有关此问题的常见问题并试图解决它,但是没有运气。我想念什么?

4 个答案:

答案 0 :(得分:1)

您正在尝试将类型为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":"$623,279,547","Production":"Walt Disney Pictures","Website":"http://marvel.com/avengers_movie","Response":"True"}]

(只需添加括号)

您必须选择哪种选项取决于您的要求,即您要允许多个对象还是只允许一个对象。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您尝试将一个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

答案 3 :(得分:0)

当我们将api称为“ http://www.omdbapi.com/?apikey=7663ce8e&t=Avenger”时,我们得到的对象不是对象数组

尝试

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

如果要查看电影列表,请尝试使用其他API,例如:themoviedbAPI