我从 OMDb API 下载了一个 JSON 字符串,现在我想反序列化为一个对象列表,但我得到了错误代码:
Newtonsoft.Json.JsonSerializationException:“无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[MovieReommendation.Movie]',因为该类型需要 JSON 数组(例如 [1,2,3])正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或将反序列化类型更改为正常的 .NET 类型(例如,不是像整数这样的原始类型,不是像数组或列表这样的集合类型)可以从 JSON 对象反序列化。也可以将 JsonObjectAttribute 添加到类型以强制它从 JSON 对象反序列化。路径“搜索” ,第 1 行,第 10 位。”
这是我的代码:
public void Import(string URL)
{
using (WebClient wc = new WebClient())
{
var jsonString = wc.DownloadString(URL);
var Json = JsonConvert.DeserializeObject<List<Movie>>(jsonString);
}
}
public class Movie
{
#region properties
[JsonProperty("Title")]
public string Title { get; set; }
[JsonProperty("Year")]
public string Year { get; set; }
[JsonProperty("Type")]
public string Type { get; set; }
[JsonProperty("imbdID")]
public int imbdID { get; set; }
[JsonProperty("Poster")]
public string Poster { get; set; }
}
{
"Search": [{
"Title": "Batman Begins",
"Year": "2005",
"imdbID": "tt0372784",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BOTY4YjI2N2MtYmFlMC00ZjcyLTg3YjEtMDQyM2ZjYzQ5YWFkXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_SX300.jpg"
}, {
"Title": "Batman v Superman: Dawn of Justice",
"Year": "2016",
"imdbID": "tt2975590",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BYThjYzcyYzItNTVjNy00NDk0LTgwMWQtYjMwNmNlNWJhMzMyXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_SX300.jpg"
}, {....