2016-11-09 88 views
0
 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來轉換轉換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 
} 

我想設置moviesresults陣列。我的解決方案(我在這裏找到的)是關於將整個json(pagem results,total_results,total_pages)設置爲movies。事實上,json的答案是一個單一的對象。

如何深入瞭解此json(轉換時)將List<Movie> movies設置爲results數組?

回答

2

爲整個響應創建一個類,並有一個電影列表。

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; } 
}