将json's数组字段转换为列表

本文关键字:字段 转换 列表 数组 json | 更新日期: 2023-09-27 18:17:14

        public List<Movie> getPopularMovies()
    {
        List<Movie> movies = null;
        var client = new HttpClient();
        var task = client.GetAsync(url)
            .ContinueWith((taskwithresponse) =>
            {
                var response = taskwithresponse.Result;
                var jsonString = response.Content.ReadAsStringAsync();
                jsonString.Wait();
                movies = JsonConvert.DeserializeObject<List<Movie>>(jsonString.Result);
            });
        task.Wait();
        return movies;
    }

Json转换

{
  "page": 1,
  "results": [
    {
      "poster_path": "/xfWac8MTYDxujaxgPVcRD9yZaul.jpg",
      "adult": false,
      "overview": "After his career is destroyed, a brilliant but arrogant surgeon gets a new lease on life when a sorcerer takes him under his wing and trains him to defend the world against evil.",
      "release_date": "2016-10-25",
      "genre_ids": [
        28,
        12,
        14,
        878
      ],
      "id": 284052,
      "original_title": "Doctor Strange",
      "original_language": "en",
      "title": "Doctor Strange",
      "backdrop_path": "/hETu6AxKsWAS42tw8eXgLUgn4Lo.jpg",
      "popularity": 55.113822,
      "vote_count": 598,
      "video": false,
      "vote_average": 6.99
    }
  ],
  "total_results": 19676,
  "total_pages": 984
}

我想将movies设置为results数组。我的解决方案(我在这里发现的)是将movies设置为整个json (pagem结果,total_results, total_pages)。实际上json的答案是一个单一的对象。

如何深入这个json(同时转换)设置List<Movie> moviesresults数组?

将json's数组字段转换为列表

为整个响应创建一个类,其中包含一个电影列表。

response = JsonConvert.DeserializeObject<JsonResponse>(jsonString.Result);
movies = response.Movies;

示例类:

public class JsonResponse {
   [JsonProperty("results")]
   public List<Movie> Movies { get; set; }
   [JsonProperty("page")]
   public int Page { get; set; }
   [JsonProperty("total_results")]
   public int TotalResults { get; set; }
   [JsonProperty("total_pages")]
   public int TotalPages { get; set; }
}
public class Movie
{
    [JsonProperty("poster_path")]
    public string PosterPath { get; set; }
    [JsonProperty("adult")]
    public bool Adule { get; set; }
    [JsonProperty("overview")]
    public string Overview { get; set; }
    [JsonProperty("release_date")]
    public string ReleaseDate { get; set; }
    [JsonProperty("genre_ids")]
    public List<int> GenreIds { get; set; }
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("original_title")]
    public string OriginalTitle { get; set; }
    [JsonProperty("original_language")]
    public string OriginalLanguage { get; set; }
    [JsonProperty("title")]
    public string Title { get; set; }
    [JsonProperty("backdrop_path")]
    public string BackdropPath { get; set; }
    [JsonProperty("popularity")]
    public double Popularity { get; set; }
    [JsonProperty("vote_count")]
    public int VoteCount { get; set; }
    [JsonProperty("video")]
    public bool Video { get; set; }
    [JsonProperty("vote_average")]
    public double VoteAverage { get; set; }
}