Jsonserializationexception, Error converting JSON

Asked

Viewed 54 times

1

I am trying to deserialize an object brought by my webApi, tested with other objects and works, but with this no The error is as follows:

Jsonserializationexception: Unexpected JSON token when Reading Datatable. Expected Startarray, got Startobject. Path ', line 1, position 1.

/* Método responsável por realizar a impressão */

    public static DataTable TabelaRateios { get; set; }

    [HttpGet]
    [Route("Rateio/Report/{Rateio_Codigo}")]
    public async Task<IActionResult> Report(int Rateio_Codigo)
    {
        int? Emp_Codigo = Services.Token.strEmp_codigo;
        var UrlApi = $"api/Rateio/imprimir/{Rateio_Codigo},{Emp_Codigo}";
        Uri BaseAdress = Services.Token.BaseAdress;
        string strToken = Services.Token.strToken;
        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(UrlApi))
            {

                response.EnsureSuccessStatusCode();
                string resul = await response.Content.ReadAsStringAsync();

                TabelaRateios = JsonConvert.DeserializeObject<DataTable>(resul);
            }
        }    
        Rateio Rateios = new Rateio();

        return View(Rateios);
    }

Method in the Webapi

    [HttpGet]
    [Route("imprimir/{Rateio_Codigo},{Emp_Codigo}")]
    public HttpResponseMessage Imprimir(double Rateio_Codigo, int Emp_Codigo)
    {
        try
        {
            var result = new HttpResponseMessage(HttpStatusCode.OK);
            var Rateios = bdprincipalEntities.Rateio.Select(
                    x => new {
                        Rateio_Codigo = (int)x.Rateio_Codigo,
                        x.Rateio_descricao,
                        x.Emp_Codigo,
                        Rateio_Situacao2 = x.Rateio_Situacao.Equals("A") ? "ATIVO" : "DESATIVADO"
                    }).FirstOrDefault(x => x.Rateio_Codigo == Rateio_Codigo && x.Emp_Codigo == Emp_Codigo); ;
            result.Content = new StringContent(JsonConvert.SerializeObject(Rateios));
            result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return result;
        }
        catch (Exception)
        {
            return new HttpResponseMessage(HttpStatusCode.BadRequest);
        }
    }

Json of the string result:

inserir a descrição da imagem aqui

"{\"Code ":2, "Description ": "VR.SALES AFTER DESC. -LD IN GRADUATION ", "Emp_codigo ":1, "Rateio_situacao2 ": "ACTIVE\"}"

  • Hello, when converting the object to String, put it inside a pair of {} keys, and check if it solved the problem.

  • That’s what you’re trying to say? resul = "{" + resul + "}";

  • This, because it seems, when converting the String to the Datatable object, it expects an Array, not the object itself.

  • Unfortunately, it did not work, it was as follows the result variable "{{\"Rateio_Codigo\":2,\"Rateio_descricao\":\"VR.VENDAS APÓS DESC.-LD EM GRADUAÇÃO\",\"Emp_Codigo\":1,\"Rateio_Situacao2\":\"ATIVO\"}}"

  • Oops, pardon me, buddy, I meant put it between brackets [ ].

  • Sorry I’m late, I’ll try

  • @Gabrielferreira It worked, very obg, could you put an answer please? And if you can explain me too, thank you

Show 2 more comments
No answers

Browser other questions tagged

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