Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Json反序列化数组_C#_Json_Json Deserialization - Fatal编程技术网

C# Json反序列化数组

C# Json反序列化数组,c#,json,json-deserialization,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 (scr

我不熟悉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"
}
var objs = JsonConvert.DeserializeObject<SomeModel[]>(json);
我可以获取数据并读取数据,但在进行反序列化时,会出现以下错误:

Newtonsoft.Json.JsonSerializationException:“无法将当前Json对象(例如{name:value})反序列化为类型”System.Collections.Generic.List`1[Deneme.Modeler.Main]”,因为该类型需要Json数组(例如[1,2,3])才能正确反序列化。 若要修复此错误,请将JSON更改为JSON数组,例如[1,2,3],或更改反序列化类型,使其成为正常的.NET类型,例如,不是integer之类的基元类型,也不是可以从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数组的,但我看了看有关这个问题的问题,并试图解决它,但没有运气。我错过了什么

Main Info = JsonConvert.DeserializeObject<Main>(getinfo);
您的json字符串只有一个主对象,您试图获取一个列表

您试图将一个json对象反序列化为一个对象列表。 这是一个简单对象的示例:

{ "field": 123 }
要对其进行反序列化,您需要:

var obj = JsonConvert.DeserializeObject<SomeModel>(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"
}
var objs = JsonConvert.DeserializeObject<SomeModel[]>(json);

您的问题的解决方案: 将反序列化类型更改为单个对象。 用[]将JSON包装起来
您正在尝试将Main类型的单个对象反序列化为对象列表

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

第一种选择是

Main Info = JsonConvert.DeserializeObject<Main>(getinfo);
只需添加括号

您必须选择哪个选项取决于您的需求,即如果您希望允许多个对象或仅允许一个对象。

当我们调用api时,我们得到的是一个对象,而不是对象数组

试一试

var info=JsonConvert.DeserializeObjectgetinfo;
如果您想要电影列表,请尝试其他api b.e.:

Json数组将始终以方括号开始[,Json对象将始终以花括号开始{.a对象!=array@Selvin哈哈,没错。当我看到这些评论时,我就像wtf一样。该死的盲目。谢谢你的回答。
[{"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"}]