Convert Json to a Csharp Object

Asked

Viewed 468 times

0

 public IActionResult MoedaTurismo()
    {
        try
        {
            RestClient restClient = new RestClient();
            RestRequest restRequest = new RestRequest();
            restRequest.AddHeader("cache-control", "no-cache");
            restRequest.AddHeader("content-Type", "application/json");
            restClient.BaseUrl = new Uri("http://economia.awesomeapi.com.br/USD-BRLT/1");

            var response = restClient.Execute(restRequest);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var json = response.Content;
                var Moeda = JsonConvert.DeserializeObject(json);

                return (object);
            }
            return NotFound();
        }
        catch (Exception ex)
        {
            return StatusCode((int)HttpStatusCode.InternalServerError);

        }

    }

I want to convert Json to C#, this correct ?

1 answer

1

First use json2csharp to map all Json fields to a class.

Model:

public class Moeda
{
  public string code { get; set; }
  public string codein { get; set; }
  public string name { get; set; }
  public string high { get; set; }
  public string low { get; set; }
  public string pctChange { get; set; }
  public string open { get; set; }
  public string bid { get; set; }
  public string ask { get; set; }
  public string varBid { get; set; }
  public string timestamp { get; set; }
  public string create_date { get; set; }
}

JSON:

[  
   {  
      "code":"USD",
      "codein":"BRLT",
      "name":"D\u00f3lar Turismo",
      "high":"3.91",
      "low":"3.75",
      "pctChange":"-0.488",
      "open":"0",
      "bid":"3.76",
      "ask":"4.08",
      "varBid":"-0.02",
      "timestamp":"1544452500000",
      "create_date":"2018-12-10 12:40:03"
   }
]

Note that the JSON brought in the url it consists of [] then it is interpreted as an array and not an object, so use a collection to deserialize JSON List<T>

Example:

 public IActionResult MoedaTurismo()
 {
            try
            {
                RestClient restClient = new RestClient();
                RestRequest restRequest = new RestRequest();
                restRequest.AddHeader("cache-control", "no-cache");
                restRequest.AddHeader("content-Type", "application/json");
                restClient.BaseUrl = new Uri("http://economia.awesomeapi.com.br/USD-BRLT/1");

                var response = restClient.Execute(restRequest);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var json = response.Content;
                    List<Moeda> obj = JsonConvert.DeserializeObject<List<Moeda>>(json);

                    return (object);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                return StatusCode((int)HttpStatusCode.InternalServerError);

            }

 }

Browser other questions tagged

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