Conversion error in Restsharp Deserialize method

Asked

Viewed 287 times

1

I have a method that consumes a Rest and then deserializes using a class. It was working, but maybe by changing the return made appear the error.

My method is like this:

public void LoadPedidos()
{
    var client = new RestClient(ConfigurationManager.AppSettings["url_client"]);
    var request = new RestRequest(ConfigurationManager.AppSettings["req_Pedidos"], Method.POST);

    request.AddParameter("loja_evento", ConfigurationManager.AppSettings["loja_evento"]);
    request.AddParameter("usuario", ConfigurationManager.AppSettings["usuario"]);

    IRestResponse response = client.Execute(request);
    JsonDeserializer deserial = new JsonDeserializer();
    var des = deserial.Deserialize<Pedidos>(response);

    int credito = int.Parse(ConfigurationManager.AppSettings["credito"]);
    int debito = int.Parse(ConfigurationManager.AppSettings["debito"]);

    Console.WriteLine(des.status);
    Console.WriteLine(des.itens);
}

And my class Orders:

public class Pagto
{
    public int id_pedido_pagamento { get; set; }
    public int id_forma_de_pagamento { get; set; }
    public decimal valor { get; set; }        
    public int parcelas { get; set; }
}

public class Iten
{
    public string pedido { get; set; }
    public string cliente { get; set; }        
    public List<Pagto> pagtos { get; set; }        
}

public class Pedidos
{
    public bool status { get; set; }
    public List<Iten> itens { get; set; }
}

However, you are returning a string type conversion error to the datetime type.

That is the mistake:

Exception thrown: 'System.FormatException' in mscorlib.dll

Additional information: A cadeia de caracteres de entrada não estava em um formato correto.

Example of Rest return:

{
"status":true,
"itens":[
  {
     "pedido":"562-SERVER_HOM",
     "cliente":"",
     "pagtos":[
        {
           "id_pedido_pagamento":"537",
           "id_forma_de_pagamento":"2",
           "valor":"189.00",
           "parcelas":"4"
        }
     ]
  },
  {
     "pedido":"564-SERVER_HOM",
     "cliente":"",
     "pagtos":[
        {
           "id_pedido_pagamento":"538",
           "id_forma_de_pagamento":"2",
           "valor":"90.00",
           "parcelas":"2"
        }
     ]
  },
  {
     "pedido":"571-SERVER_HOM",
     "cliente":"",
     "pagtos":[
        {
           "id_pedido_pagamento":"539",
           "id_forma_de_pagamento":"5",
           "valor":"189.00"
        }
     ]
  }
]
}
  • 1

    On which line is the error?

  • var des = deserialize. Deserialize<Orders>(Answer);

  • What is the item he is processing when he gives the error?

No answers

Browser other questions tagged

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