【问题标题】:Json Deserialize ArrayJson 反序列化数组
【发布时间】:2019-10-04 08:27:34
【问题描述】:

我是 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]' 因为type 需要一个 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 数组的,但我查看了有关此问题的问题并尝试修复它但没有运气。我错过了什么?

【问题讨论】:

  • 一个 Json array 将始终以方括号 [ 开头,Json object 将始终以大括号 { 开头。 对象 != 数组
  • @Selvin LOL 没错。当我看到cmets该死的失明时,我就像wtf一样。感谢您的回答。

标签: c# json json-deserialization


【解决方案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"}]

(只需添加括号)

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

【讨论】:

    【解决方案2】:
    Main Info = JsonConvert.DeserializeObject<Main>(getinfo);
    

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        试试

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

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

        【讨论】:

          猜你喜欢
          • 2015-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-24
          • 2017-01-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多