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();
}
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?
– Eduardo Nogueira
result is the class in my example
Rootobject
which in your case isJson
(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 variableresult
has all the information– novic
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?
– Eduardo Nogueira
@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
– novic
OK. Thanks for the help.
– Eduardo Nogueira