Error deserializing json

Asked

Viewed 141 times

0

I have a little problem with deserializar mine json, and I don’t know why

    public async Task<IActionResult> Index()
    {
        Uri BaseAdress = Services.Token.BaseAdress;
        string strToken = Services.Token.strToken;
        List<FormaPagamentoFinModel> ListaAmbientes;
        using (HttpClient httpClient = new HttpClient())
        {
            httpClient.BaseAddress = BaseAdress;
            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", strToken);
            httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("Application/Json"));
            using (HttpResponseMessage response = await httpClient.GetAsync("/api/FormaPagamentoFin/findAll"))
            {

                response.EnsureSuccessStatusCode();
                string resul = await response.Content.ReadAsStringAsync();
                ListaAmbientes = JsonConvert.DeserializeObject<List<FormaPagamentoFinModel>>(resul);
            }
        }
        return View(ListaAmbientes);
    }

My variable resul receives the normal data, but Deserializar he returns to me everything null, he returns me the count correctly, but all variables with null value

inserir a descrição da imagem aqui

Findall method in API:

    [HttpGet]
    [Route("findAll")]
    public HttpResponseMessage findAll()
    {
        try
        {
            var result = new HttpResponseMessage(HttpStatusCode.OK);
            var ambientes = bdprincipalEntities.forma_pagamento_fin.Select(
                    x => new {
                        Fpg_codigo = x.Fpf_codigo,
                        Fpg_descricao = x.Fpf_descricao,
                        Fpg_quantidade = x.Fpf_quantidade,
                        Fpg_situacao = x.Fpf_situacao.Equals("A") ? "ATIVO" : "DESATIVADO"
                    }).ToList();
            result.Content = new StringContent(JsonConvert.SerializeObject(ambientes));
            result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return result;
        }
        catch (Exception)
        {
            return new HttpResponseMessage(HttpStatusCode.BadRequest);
        }
    }
  • 1

    The model FormaPagamentoFinModel is the same returned by the method /api/FormaPagamentoFin/findAll ? How is this method FindAll?

  • I’ll ask the question, sorry

  • Wow, I’ve touched on the bug just now, I’m passing wrong on the api

  • Post an answer I accept

1 answer

2


In the code posted, the object definition in the method FindAll is different from the object FormaPagamentoFinModel.

An example would be to change Fpg_codigofor Fpf_codigo as well as other properties with different names.

Browser other questions tagged

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