How to consume data from an api to show on the front end using Razor pages?

Asked

Viewed 216 times

1

I’m a beginner in development. net core and I am developing an application that consumes data from an api and shows to users, but I can’t convert JSON to OBJECT and crunch everything into a Ienumerable.

I am using REFIT to make HTTP requests.

Model class:

  public class Json
  {
    [JsonProperty("Message")]
    public string Message { get; set; }

    [JsonProperty("Global")]
    public Global Global { get; set; }

    [JsonProperty("Countries")]
    public Country[] Countries { get; set; }
  }

  public class Global
  {
    [JsonProperty("NewConfirmed")]
    public long NewConfirmed { get; set; }

    [JsonProperty("TotalConfirmed")]
    public long TotalConfirmed { get; set; }

    [JsonProperty("NewDeaths")]
    public long NewDeaths { get; set; }

    [JsonProperty("TotalDeaths")]
    public long TotalDeaths { get; set; }

    [JsonProperty("NewRecovered")]
    public long NewRecovered { get; set; }

    [JsonProperty("TotalRecovered")]
    public long TotalRecovered { get; set; }
  }
  public class Country
  {
    [JsonProperty("Country")]
    public string CountryCountry { get; set; }

    [JsonProperty("CountryCode")]
    public string CountryCode { get; set; }

    [JsonProperty("Slug")]
    public string Slug { get; set; }

    [JsonProperty("NewConfirmed")]
    public long NewConfirmed { get; set; }

    [JsonProperty("TotalConfirmed")]
    public long TotalConfirmed { get; set; }

    [JsonProperty("NewDeaths")]
    public long NewDeaths { get; set; }

    [JsonProperty("TotalDeaths")]
    public long TotalDeaths { get; set; }

    [JsonProperty("NewRecovered")]
    public long NewRecovered { get; set; }

    [JsonProperty("TotalRecovered")]
    public long TotalRecovered { get; set; }

    [JsonProperty("Date")]
    public DateTimeOffset Date { get; set; }

    [JsonProperty("Premium")]
    public Premium Premium { get; set; }
  }

  public class Premium
  {
  }

Class Indexmodel:

public class IndexModel : PageModel
  {
    private readonly ILogger<IndexModel> _logger;
    public IEnumerable<Json> Cities { get; private set; } = new List<Json>();

    public IndexModel(ILogger<IndexModel> logger)
    {
      _logger = logger;
    }

    public async void OnGet()
    {
      try
      {
        var city = RestService.For<IJsonApiService>("https://api.covid19api.com/summary");
        Cities = (IEnumerable<Json>)await city.GetCity().ConfigureAwait(false);
      }
      catch (Exception e)
      {
        _logger.LogInformation("Erro na requisição http: " + e.Message);
      }
    }
  }

Interface Ijsonapiservice:

public interface IJsonApiService
{
   [Get("")]
   Task<Json> GetCity();
}

1 answer

0


Relevant remark: that one returns an object that in its members has other objects and one of them is a list of cities, so there is no way to return for this what is asked and I decided to put the appropriate example by the correct return of this .


The layout of the base classes to load information from sent by the request is the following:

public class Rootobject
{
    public string Message { get; set; }
    public Global Global { get; set; }
    public Country[] Countries { get; set; }
    public DateTime Date { get; set; }
}

public class Global
{
    public int NewConfirmed { get; set; }
    public int TotalConfirmed { get; set; }
    public int NewDeaths { get; set; }
    public int TotalDeaths { get; set; }
    public int NewRecovered { get; set; }
    public int TotalRecovered { get; set; }
}

public class Country
{
    [JsonProperty("Country")]
    public string Name { get; set; }
    public string CountryCode { get; set; }
    public string Slug { get; set; }
    public int NewConfirmed { get; set; }
    public int TotalConfirmed { get; set; }
    public int NewDeaths { get; set; }
    public int TotalDeaths { get; set; }
    public int NewRecovered { get; set; }
    public int TotalRecovered { get; set; }
    public DateTime Date { get; set; }
    public Premium Premium { get; set; }
}

public class Premium
{
}

and to work with refit a for consumption of as follows:

public interface IGetRootobject
{
    [Get("")]
    Task<Rootobject> GetAsync();
}

and finally to use all the above codes and to consume :

var create = RestService.For<IGetRootobject>("https://api.covid19api.com/summary");
var result = create.GetAsync().GetAwaiter().GetResult();
// ou var result = await create.GetAsync();

whereas the values of result is the return of .

Reference: refit

  • If the var result return is from the api, then does the result contain json? How do I turn this json into Object to show on the front end?

  • result is the class in my example Rootobject which in your case is Json (this name is not good change), in return is already the ready object mounting with the information @Eduardonogueira take this code and test for you to see that in the variable result has all the information

  • I implemented it here. Now I need to go through this Rootobject to get the data, but first I need to go through Countries. How do I do this?

  • @Eduardonogueira each question generates an answer, if it can create another question for this new doubt, and another thing if this answered you the initial doubt vote and signal as answer to your question

  • 1

    OK. Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.