Classe Car:
using System.Text.Json.Serialization;
namespace exercise95.Models;
internal class Car
{
[JsonPropertyName("marca")]
public string? Brand { get; set; }
[JsonPropertyName("modelo")]
public string? Model { get; set; }
[JsonPropertyName("ano")]
public int? Year { get; set; }
[JsonPropertyName("tipo")]
public string? Type { get; set; }
public string DetailedDescription => $"Brand: {Brand} - Model: {Model} - Year: {Year} - Type: {Type}";
}
Program:
using exercise95.Models;
using System.Text.Json;
using (HttpClient client = new HttpClient())
{
try
{
string response = await client.GetStringAsync("https://raw.githubusercontent.com/ArthurOcFernandes/Exerc-cios-C-/curso-4-aula-2/Jsons/Carros.json");
List<Car> cars = JsonSerializer.Deserialize<List<Car>>(response)!;
foreach (Car car in cars)
{
Console.WriteLine(car.DetailedDescription);
}
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}