Error while converting JSON

Asked

Viewed 154 times

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.

  • Note that the property value Con_codigo in the JSON is not a string. Is a decimal.

  • From the error, it said Input string '42.0', I thought it was considering as a string, thank you

1 answer

3


Jeff, I believe the class to which your object belongs "Contact lists" own a name property "Code". This property needs to be to type decimal to receive this kind of value.

  • She’s kind of int, I’ll edit post

  • However, it is receiving as if it were string "42.0" and should receive only "42"

  • @Jeffhenrique In JSON it is not a string, it is a decimal.

  • I understand, you are sure that it would not be sending this data that would already be this way before arriving at your application?

  • Oh yes, so I have to do a treatment, I’ll get to see how it does

  • @Jeffhenrique Have you checked in the service that provides the json await httpClient.GetAsync(UrlApi2)) Probably it sends a decimal, because if the same one was sending whole would send only 42

  • @Jeffhenrique, the problem is not your View Model but what the API returns

  • @Julioborges vc is right, in the api the value is double, and in my project is int, I will change, I thought it was error when converting json, thanks

  • @Jeffhenrique, blz, accept Sérgio Lopes' answer, because he was correct

  • I’ll take it, I’m just testing to see if it works

  • It worked, thank you very much Sérgiolopes, @Julioborges and Linq

  • It was a pleasure to help!

Show 7 more comments

Browser other questions tagged

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