Resolução:
Program.cs
using DesafiosCSharp03._02Desafio.Modelos;
using System.Text.Json;
using (HttpClient client = new HttpClient())
{
try
{
string resposta = await client.GetStringAsync("https://raw.githubusercontent.com/ArthurOcFernandes/Exerc-cios-C-/curso-4-aula-2/Jsons/TopMovies.json");
var filmes = JsonSerializer.Deserialize<List<Filme>>(resposta)!;
foreach (var filme in filmes)
{
filme.ExibirInformacoes();
}
}
catch (Exception ex)
{
Console.WriteLine($"Ocorreu um erro ao obter os dados: {ex.Message}");
}
}
Filme.cs
using System.Text.Json.Serialization;
namespace DesafiosCSharp03._02Desafio.Modelos
{
internal class Filme
{
[JsonPropertyName("rank")]
public string? Rank { get; set; }
[JsonPropertyName("title")]
public string? Nome { get; set; }
[JsonPropertyName("year")]
public string? Ano { get; set; }
[JsonPropertyName("crew")]
public string? Elenco { get; set; }
[JsonPropertyName("imDbRating")]
public string? Avaliacao { get; set; }
[JsonPropertyName("imDbRatingCount")]
public string? ContagemAvaliacoes { get; set; }
public void ExibirInformacoes()
{
Console.WriteLine($"Rank: {Rank}");
Console.WriteLine($"Título: {Nome}");
Console.WriteLine($"Ano: {Ano}");
Console.WriteLine($"Avaliação: {Avaliacao}");
Console.WriteLine($"Contagem de Avaliações: {ContagemAvaliacoes}");
Console.WriteLine(new string('-', 30));
}
}
}