Carro.cs
using System.Text.Json.Serialization;
namespace Automoveis.Modelos;
class Carro {
[JsonPropertyName("marca")]
public string? Marca {get;set;}
[JsonPropertyName("modelo")]
public string? Modelo {get;set;}
[JsonPropertyName("ano")]
public int Ano {get;set;}
[JsonPropertyName("tipo")]
public string? Tipo {get;set;}
[JsonPropertyName("motor")]
public string? Motor {get;set;}
[JsonPropertyName("transmissao")]
public string? Transmissao {get;set;}
public void ExibirDetalhes() {
Console.WriteLine();
Console.WriteLine($"Marca: {Marca}");
Console.WriteLine($"Modelo: {Modelo}");
Console.WriteLine($"Ano do carro: {Ano}");
Console.WriteLine($"Tipo: {Tipo}");
Console.WriteLine($"Motor do carro: {Motor}");
Console.WriteLine($"Tipo de tranmissão: {Transmissao}");
Console.WriteLine();
}
}
Program.cs
using System.Text.Json;
using Automoveis.Modelos;
Console.Clear();
using (HttpClient client = new()) {
try {
var response = await client.GetStringAsync("https://raw.githubusercontent.com/ArthurOcFernandes/Exerc-cios-C-/curso-4-aula-2/Jsons/Carros.json");
var listaCarros = JsonSerializer.Deserialize<List<Carro>>(response);
listaCarros!.ForEach(c => c.ExibirDetalhes());
}
catch (Exception ex) {
Console.WriteLine($"Um erro foi encontrado na requisição: {ex}");
}
}