elzoy elzoy - 29 days ago 19
C# Question

Converting json's array field to list

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 to convert

{
"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
}


I'd like to set
movies
as
results
array. My solution (what I found here) is about setting
movies
as the whole json (pagem results, total_results, total_pages). In fact the json answer is a single object.

How to get deeply inside this json (while converting) to set the
List<Movie> movies
to
results
array?

Answer

Create a class for the entire response, with a list of movies on it.

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

Example classes:

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