使用 JSON 反序列化

Deserialize with JSON

我从 OMDb API 下载了一个 JSON 字符串,现在我想反序列化为一个对象列表,但我得到错误代码:

Newtonsoft.Json.JsonSerializationException: "Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MovieReommendation.Movie]' 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. Path 'Search', line 1, position 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"
    }, {....

您需要 Root class.

public class Root
{
    public List<Movie> Search { get; set; }
}

public class Movie
{
    [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; }
}

并且您需要将 JSON 反序列化为 Root 对象。

public void Import(string URL)
{
    using (WebClient wc = new WebClient())
    {
        var jsonString = wc.DownloadString(URL);
        var json = JsonConvert.DeserializeObject<Root>(jsonString);
        List<Movie> movies = json.Search;
    }
}

仅供参考,您可以根据 JSON 结果生成 classes json2csharp

尝试定义一个 class 如下

  • 将 json 复制到 windows 剪贴板
  • 在您的项目中创建一个新的 class
  • 从Visual Studio编辑菜单selectpaste special,Paste Json as class进入class刚创建的
  • 去掉原来的class
  • 在下面的示例中,MoviesRoot 是 RootObject

现在当 DeserializeObject 时,将其键入 MoviesRoot 就可以了

public class MoviesRoot
{
    public Search[] Search { get; set; }
}

public class Search
{
    public string Title { get; set; }
    public string Year { get; set; }
    public string imdbID { get; set; }
    public string Type { get; set; }
    public string Poster { get; set; }
}

请注意,我注意到另一个回复是发帖,所以主要区别在于在 Visual Studio 内部转换 json 或使用第三方工具。

您的 json 不是数组。它是一个带有数组键的对象。这两者是有区别的类。您的 json 代表第一个,而不是第二个。第二个是您尝试将其序列化的内容。

public class Search {
List<Movie> search ; 
}
List<Movie> movies;