【问题标题】:Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON objectNewtonsoft.Json.JsonSerializationException:无法反序列化当前 JSON 对象
【发布时间】:2016-10-16 01:54:16
【问题描述】:

我有以下 JSON 我正在获取网络服务。

{
  "CodigoRetorno": 0,
  "Mensagem": "Sucesso",
  "Dados": {
    "Agendas": [
      {
        "CodigoClinica": 6259,
        "CodigoConvenio": 3078,
        "CodigoPaciente": 96589,
        "CodigoProfissional": 9732,
        "CodigoTipoAgendamento": 1,
        "CodigoProcedimento": 2047,
        "DataAgenda": "2016-10-10T00:00:00",
        "DataAlteracao": "2016-10-10T12:08:44.367",
        "DataInclusao": "2016-10-10T12:03:23.25",
        "HoraFim": "14:25:00",
        "HoraInicio": "14:00:00",
        "Observacao": null,
        "Status": 1,
        "StatusDescricao": "Não Confirmado",
        "Cor": "#659be0",
        "NomePaciente": "Murilo neandro",
        "NomeProfissional": "Fernanda valente",
        "NomeProcedimento": null,
        "CodigoProfissionalUsuario": 10260,
        "Codigo": 291193,
        "Excluido": false
      }
    ],
    "Profissionais": [
      {
        "Nome": "Fernanda valente",
        "Apelido": null,
        "CNPJOuCPF": null,
        "CodigoClinica": 6259,
        "CodigoTipoDocProfissional": null,
        "Documento": null,
        "CEP": null,
        "Endereco": null,
        "Complemento": null,
        "Numero": null,
        "Bairro": null,
        "Municipio": null,
        "UF": null,
        "CodigoTipoProfissional": 2,
        "DocumentoUF": "SP",
        "Email": "viperconsultoria@gmail.com",
        "DataNascimento": null,
        "Sexo": null,
        "Observacao": null,
        "Faltas": 0,
        "Atendimentos": 0,
        "CodigoConselho": 1,
        "CRM": "455451",
        "Codigo": 9732,
        "Excluido": false
      }
    ],
    "Telefones": [
      {
        "CodigoClinica": 6259,
        "NumeroTelefone": "65955292552",
        "CodigoTipoTelefone": 0,
        "Codigo": 5170,
        "Excluido": false
      }
    ],
    "Convenios": [
      {
        "CodigoClinica": 6259,
        "CodigoSequencial": 1,
        "Nome": "Particular",
        "Cor": "#2d8b46",
        "Codigo": 3078,
        "Excluido": false
      }
    ],
    "Pacientes": [
      {
        "Bairro": null,
        "CEP": null,
        "CPF": null,
        "Celular": "89589589859",
        "CodigoClinica": 6259,
        "Complemento": null,
        "Email": null,
        "Endereco": null,
        "Municipio": null,
        "Nome": "Murilo neandro",
        "Numero": null,
        "Telefone": "98985859859",
        "UF": null,
        "DataNascimento": null,
        "Faltas": null,
        "Sexo": " ",
        "LiberaAtendimento": false,
        "Codigo": 96589,
        "Excluido": false
      }
    ],
    "FinanceiroCategorias": [
      {
        "CodigoClinica": 6259,
        "Descricao": "Ajustes",
        "Codigo": 1,
        "Excluido": false
      }
    ],
    "FinanceiroSubCategorias": [
      {
        "CodigoClinicaCategoria": 6,
        "CodigoClinica": 6259,
        "Descricao": "Procedimento",
        "Tipo": "R",
        "Cor": "#659be0",
        "Codigo": 63,
        "Excluido": false
      }
    ],
    "FormaPagamentos": [
      {
        "CodigoClinica": 6259,
        "CodigoFormaPagamento": 1,
        "CodigoFormaPagamentoPai": null,
        "CodigoUsuario": 1,
        "Nome": "CAIXA",
        "Ativo": 1,
        "DataAlteracao": "2016-10-10T11:49:56.41",
        "Codigo": 86,
        "Excluido": false
      }
    ],
    "Bairro": "Vila Ana Maria",
    "CEP": "14026220",
    "CNES": null,
    "CNPJCPF": "65625656256256",
    "Complemento": null,
    "Email": null,
    "Endereco": "Rua Raul Peixoto",
    "Municipio": "Ribeirão Preto",
    "Nome": "Viper Clinicas",
    "Numero": "630",
    "UF": "SP",
    "HoraInicio": "08:00:00",
    "HoraFim": "20:00:00",
    "Latitude": "-21.2103565",
    "Longitude": "-47.8183606",
    "Codigo": 6259,
    "Excluido": false
  }
}

我用代码反序列化

var clinicas = JsonConvert.DeserializeObject<List<Clinica>>(response.Content);

只有我有如下图所示的错误 Error

我使用我正在使用的课程来更好地了解我的情况

public class Clinica : ModelBase
    {
        private string _nome;
        private double _latitude;
        private double _longitude;

        public string Nome
        {
            get
            {
                return _nome;
            }
            set
            {
                _nome = value;
                RaisedPropertyChanged(() => Nome);
            }
        }

        public double Latitude
        {
            get
            {
                return _latitude;
            }
            set
            {
                _latitude = value;
                RaisedPropertyChanged(() => Latitude);
            }   
        }

        public double Longitude
        {
            get
            {
                return _longitude;
            }
            set
            {
                _longitude = value;
                RaisedPropertyChanged(() => Longitude);
            }
        }


    }

我该如何解决这个问题?

【问题讨论】:

  • JSON 与您尝试反序列化的类型不匹配。甚至没有关闭。
  • 我将 Clinica 类的名称更改为 Dados 名称,但出现了同样的错误。我只需要名称属性,纬度,经度

标签: c# json xamarin json.net


【解决方案1】:

有时像:

var example = JsonConvert.DeserializeObject<Example>(jsonString);
Console.WriteLine(example.Dados.Latitude);

如果您的类模型如下所示:

public class Agenda
{
    public int CodigoClinica { get; set; }
    public int CodigoConvenio { get; set; }
    public int CodigoPaciente { get; set; }
    public int CodigoProfissional { get; set; }
    public int CodigoTipoAgendamento { get; set; }
    public int CodigoProcedimento { get; set; }
    public DateTime DataAgenda { get; set; }
    public DateTime DataAlteracao { get; set; }
    public DateTime DataInclusao { get; set; }
    public string HoraFim { get; set; }
    public string HoraInicio { get; set; }
    public object Observacao { get; set; }
    public int Status { get; set; }
    public string StatusDescricao { get; set; }
    public string Cor { get; set; }
    public string NomePaciente { get; set; }
    public string NomeProfissional { get; set; }
    public object NomeProcedimento { get; set; }
    public int CodigoProfissionalUsuario { get; set; }
    public int Codigo { get; set; }
    public bool Excluido { get; set; }
}

public class Profissionai
{
    public string Nome { get; set; }
    public object Apelido { get; set; }
    public object CNPJOuCPF { get; set; }
    public int CodigoClinica { get; set; }
    public object CodigoTipoDocProfissional { get; set; }
    public object Documento { get; set; }
    public object CEP { get; set; }
    public object Endereco { get; set; }
    public object Complemento { get; set; }
    public object Numero { get; set; }
    public object Bairro { get; set; }
    public object Municipio { get; set; }
    public object UF { get; set; }
    public int CodigoTipoProfissional { get; set; }
    public string DocumentoUF { get; set; }
    public string Email { get; set; }
    public object DataNascimento { get; set; }
    public object Sexo { get; set; }
    public object Observacao { get; set; }
    public int Faltas { get; set; }
    public int Atendimentos { get; set; }
    public int CodigoConselho { get; set; }
    public string CRM { get; set; }
    public int Codigo { get; set; }
    public bool Excluido { get; set; }
}

public class Telefone
{
    public int CodigoClinica { get; set; }
    public string NumeroTelefone { get; set; }
    public int CodigoTipoTelefone { get; set; }
    public int Codigo { get; set; }
    public bool Excluido { get; set; }
}

public class Convenio
{
    public int CodigoClinica { get; set; }
    public int CodigoSequencial { get; set; }
    public string Nome { get; set; }
    public string Cor { get; set; }
    public int Codigo { get; set; }
    public bool Excluido { get; set; }
}

public class Paciente
{
    public object Bairro { get; set; }
    public object CEP { get; set; }
    public object CPF { get; set; }
    public string Celular { get; set; }
    public int CodigoClinica { get; set; }
    public object Complemento { get; set; }
    public object Email { get; set; }
    public object Endereco { get; set; }
    public object Municipio { get; set; }
    public string Nome { get; set; }
    public object Numero { get; set; }
    public string Telefone { get; set; }
    public object UF { get; set; }
    public object DataNascimento { get; set; }
    public object Faltas { get; set; }
    public string Sexo { get; set; }
    public bool LiberaAtendimento { get; set; }
    public int Codigo { get; set; }
    public bool Excluido { get; set; }
}

public class FinanceiroCategoria
{
    public int CodigoClinica { get; set; }
    public string Descricao { get; set; }
    public int Codigo { get; set; }
    public bool Excluido { get; set; }
}

public class FinanceiroSubCategoria
{
    public int CodigoClinicaCategoria { get; set; }
    public int CodigoClinica { get; set; }
    public string Descricao { get; set; }
    public string Tipo { get; set; }
    public string Cor { get; set; }
    public int Codigo { get; set; }
    public bool Excluido { get; set; }
}

public class FormaPagamento
{
    public int CodigoClinica { get; set; }
    public int CodigoFormaPagamento { get; set; }
    public object CodigoFormaPagamentoPai { get; set; }
    public int CodigoUsuario { get; set; }
    public string Nome { get; set; }
    public int Ativo { get; set; }
    public DateTime DataAlteracao { get; set; }
    public int Codigo { get; set; }
    public bool Excluido { get; set; }
}

public class Dados
{
    public IList<Agenda> Agendas { get; set; }
    public IList<Profissionai> Profissionais { get; set; }
    public IList<Telefone> Telefones { get; set; }
    public IList<Convenio> Convenios { get; set; }
    public IList<Paciente> Pacientes { get; set; }
    public IList<FinanceiroCategoria> FinanceiroCategorias { get; set; }
    public IList<FinanceiroSubCategoria> FinanceiroSubCategorias { get; set; }
    public IList<FormaPagamento> FormaPagamentos { get; set; }
    public string Bairro { get; set; }
    public string CEP { get; set; }
    public object CNES { get; set; }
    public string CNPJCPF { get; set; }
    public object Complemento { get; set; }
    public object Email { get; set; }
    public string Endereco { get; set; }
    public string Municipio { get; set; }
    public string Nome { get; set; }
    public string Numero { get; set; }
    public string UF { get; set; }
    public string HoraInicio { get; set; }
    public string HoraFim { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public int Codigo { get; set; }
    public bool Excluido { get; set; }
}

public class Example
{
    public int CodigoRetorno { get; set; }
    public string Mensagem { get; set; }
    public Dados Dados { get; set; }
}

【讨论】:

    【解决方案2】:
    1. JSON 是一个对象,而不是一个数组。因此,您可以将其反序列化为一个对象,而不是 List&lt;&gt; 的对象。
    2. 要将其反序列化到的类必须与 JSON 的结构相匹配。否则无法匹配属性。

    类似这样的:

    public class Clinica : ModelBase
    {
        private Dados _dados;
    
        public string Dados
        {
            get
            {
                return _dados;
            }
            set
            {
                _dados = value;
                RaisedPropertyChanged(() => Dados);
            }
        }
    }
    
    public class Dados
    {
        // properties for Nome, Latitude, Longitude
    }
    

    基本上,序列化程序不会知道您希望在此处使用其他一些属性中的属性。结构(包括任何包含结构)必须匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      相关资源
      最近更新 更多