1
While converting my data I get the following error:
Input string '42.0' is not a Valid integer
This error happens because I get a value string
"42.0" in a field int
-> Con_codigo
How can I solve??
My code:
using (HttpResponseMessage response = await httpClient.GetAsync(UrlApi2))
{
response.EnsureSuccessStatusCode();
string resul = await response.Content.ReadAsStringAsync();
ListaContatos = JsonConvert.DeserializeObject<List<ContatosViewModel>>(resul);
}
Meu Json:
[
{
"Con_tpcadastro": "BAN",
"Con_codigo": 42.0,
"Con_nome": "ASD",
"Con_vinculo": "SAD",
"Con_fone": "ASD",
"Con_fax": "ASD",
"Con_celular": "ASDAS",
"Con_email": "DAS"
},
{
"Con_tpcadastro": "BAN",
"Con_codigo": 42.0,
"Con_nome": "FDS",
"Con_vinculo": "SDF",
"Con_fone": "FSD",
"Con_fax": "FSD",
"Con_celular": "FSD",
"Con_email": "SDF"
}
]
Contacts:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace SoftluxWebCore.ViewModels.Tabelas.Financeiro
{
public class ContatosViewModel
{
public string Con_tpcadastro { get; set; }
[Display(Name = "Código")]
public int? Con_codigo { get; set; }
[Display(Name = "Nome")]
[Required(ErrorMessage ="Informe o Nome do Contato")]
public string Con_nome { get; set; }
[Display(Name = "Vinculo")]
public string Con_vinculo { get; set; }
[Display(Name = "Fone")]
public string Con_fone { get; set; }
[Display(Name = "FAX")]
public string Con_fax { get; set; }
[Display(Name = "Celular")]
public string Con_celular { get; set; }
[Display(Name = "Email")]
public string Con_email { get; set; }
}
}
Or you deserialize the file (or only the property, if possible) on your own and do the treatment not to consider dot numbers as decimals. Or you desirialize it to a decimal property and then do the treatment and convert to integer. The second option seems much easier.
– Jéf Bueno
Note that the property value
Con_codigo
in the JSON is not a string. Is a decimal.– Jéf Bueno
From the error, it said Input string '42.0', I thought it was considering as a string, thank you
– Jeff Henrique