Convert Json to a Csharp Object [closed]

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 #, is this correct?

    
asked by anonymous 10.12.2018 / 15:21

1 answer

1

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

Template:

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 is composed of [] so 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);

            }

 }
    
10.12.2018 / 15:51