Classe Book:
using System.Text.Json.Serialization;
namespace exercise96.Models;
internal class Book
{
[JsonPropertyName("titulo")]
public string? Title { get; set; }
[JsonPropertyName("autor")]
public string? Author { get; set; }
[JsonPropertyName("ano_publicacao")]
public int? YearPublication { get; set; }
[JsonPropertyName("genero")]
public string? Gender { get; set; }
[JsonPropertyName("paginas")]
public int? Pages { get; set; }
public string DetailedDescription => $"Title: {Title} - Author: {Author} - Year of Publication {YearPublication} - Gender: {Gender} - Quantity Pages {Pages}";
}
Program:
using exercise96.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/Livros.json");
List<Book> books = JsonSerializer.Deserialize<List<Book>>(response)!;
foreach (Book book in books)
{
Console.WriteLine(book.DetailedDescription);
}
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}