简体   繁体   English

Newtonsoft.Json.JsonSerializationException:无法反序列化当前的JSON对象

[英]Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object

I have the following JSON I'm getting a webservice. 我有以下JSON我正在获得一个Web服务。

{
  "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
  }
}

I used the code to deserialize 我使用代码反序列化

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

Only I have the error as the image below Error 只有我有错误,因为下面的图像错误

I put the class that I am using to better understanding of my situation 我把我正在使用的课程用于更好地理解我的情况

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);
            }
        }


    }

How can I solve this? 我怎么解决这个问题?

  1. The JSON is an object, not an array. JSON是一个对象,而不是一个数组。 So you'd deserialize it to an object, not a List<> of objects. 因此,您将其反序列化为对象,而不是对象的List<>
  2. The class to which you want to deserialize it has to match the structure of the JSON. 要将其反序列化的类必须与JSON的结构相匹配。 Otherwise it won't be able to match up the properties. 否则它将无法匹配属性。

Something like this: 像这样的东西:

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
}

Basically, the serializer isn't going to know that you want the properties inside of some other properties to be used here. 基本上,序列化程序不会知道您希望在此处使用某些其他属性中的属性。 The structure, including any containing structures, has to match. 结构,包括任何包含结构,必须匹配。

Sometime like: 有时像:

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

If your class model looked like this: 如果您的班级模型如下所示:

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; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Newtonsoft.Json.JsonSerializationException:&#39;无法反序列化当前JSON对象 - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object Newtonsoft.Json.JsonSerializationException: &#39;无法反序列化当前的 JSON 数组 - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array Newtonsoft.Json.JsonSerializationException: '无法反序列化当前的 JSON object (例如 {"name":"value"}) - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) Newtonsoft.Json.JsonSerializationException - Newtonsoft.Json.JsonSerializationException 错误:Newtonsoft.Json.JsonSerializationException - Error: Newtonsoft.Json.JsonSerializationException Xamarin Newtonsoft.Json.JsonSerializationException: - Xamarin Newtonsoft.Json.JsonSerializationException: 尝试将 Json 反序列化为对象时,出现此异常“Newtonsoft.Json.JsonSerializationException” - Getting this exception "Newtonsoft.Json.JsonSerializationException", when trying to deserialize Json to object 尝试使用模型反序列化时出现 Newtonsoft.Json.JsonSerializationException 问题 - Newtonsoft.Json.JsonSerializationException problem while trying to deserialize with a model 反序列化JSON对象会引发Newtonsoft.Json.JsonSerializationException - Deserializing JSON object throws a Newtonsoft.Json.JsonSerializationException JsonSerializationException:无法反序列化当前的 JSON 对象 - JsonSerializationException: Cannot deserialize the current JSON object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM