Newtonsoft JSON Deserialize List

Asked

Viewed 389 times

0

I’m tantalizing to deserialize a JSON file, may I’m having this error "Newtonsoft.Json.Jsonserializationexception: 'Cannot deserialize the Current JSON Object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Consoleapp1.Program+detail]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly."

{
"detalhes": [],
"subdetalhes": {
    "carro": {
        "descricao": "carro",
        "detalhes": [
            {
                "codigo": 00000,
                "descricao": "CREDENCIADO",
                "data": "2019-01-15 01:05:36",
                "ticket": "00000",
                "mensal": true,
                "terminal": {
                    "codigo": 5,
                    "tipo": ""
                },
                "setor": {
                    "descricao": "ESTACIONAMENTO"
                },
                "tipoVeiculo": {
                    "descricao": "AUTOMOVEL"
                },
                "tipoServico": {
                    "descricao": "COMUM"
                }
            },
            }
        ],
        "subdetalhes": {},
        "quantidade": 1745
    },
    "caminhao": {
        "descricao": "caminhao",
        "detalhes": [],
        "subdetalhes": {},
        "quantidade": 0
    },
    "moto": {
        "descricao": "moto",
        "detalhes": [],
        "subdetalhes": {},
        "quantidade": 0
    }
},
"quantidade": 1745

}

This is the JSON file, but it has more than one [] and I’m not able to create classes to deserialize

      public class detalhe{

      public List<Entrada> entrada { get; set; }

      }

      public class Entrada{

        public int codigo { get; set; }
        public string descricao { get; set; }
        public string data { get; set; }
        public int ticket { get; set; }
        public bool mensal { get; set; }
      }
        {
            var client = new RestClient("https://xxxxxxxxxxxxxxxxxxxxxxxxxxxr/pxxxxxxxx/xxxxxxx/xxxxxx/xxxxxxx/51/20190101/xxxxxx/?detalhe=true");
            client.Timeout = -1;
            var request = new RestRequest(Method.GET);
            request.AddHeader("Authorization", "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==");
            IRestResponse response = client.Execute(request);
            //Console.WriteLine(response.Content);

            var records = JsonConvert.DeserializeObject<List<detalhe>>(response.Content);


            foreach (var r in records)

            {
                Console.Read();
                Console.WriteLine(response.Content);
            }
        } ```
  • Your classes do not match anything with the json presented... as you expect to perform this operation without doing manual mapping?

  • You can map your class with the help of online services, http://json2csharp.com/

1 answer

0


First the Json that you presented is wrong. That last } is left over

"detalhes": [
    {
        "codigo": 00000,
        "descricao": "CREDENCIADO",
        "data": "2019-01-15 01:05:36",
        "ticket": "00000",
        "mensal": true,
        "terminal": {
            "codigo": 5,
            "tipo": ""
        },
        "setor": {
            "descricao": "ESTACIONAMENTO"
        },
        "tipoVeiculo": {
            "descricao": "AUTOMOVEL"
        },
        "tipoServico": {
            "descricao": "COMUM"
        }
    },
    } <----

Now if you don’t know how to reflect this json in your respective classes for serialization and deserialization, you can use Visual Studio to do this... just copy the text from Json and use the "Special Necklace"

inserir a descrição da imagem aqui

The result for the corrected Json would be the classes below... the Rootobject and the later structures, starting from this you are correcting to actually adjust the reflection from this starting point. You will get a much better result if to perform this operation use a document 100% filled, as came empty items it ends up creating duplicates, as the Subdetalhes1, Subdetalhes2, Subdetalhes3...

public class Rootobject
{
    public object[] detalhes { get; set; }
    public Subdetalhes subdetalhes { get; set; }
    public int quantidade { get; set; }
}

public class Subdetalhes
{
    public Carro carro { get; set; }
    public Caminhao caminhao { get; set; }
    public Moto moto { get; set; }
}

public class Carro
{
    public string descricao { get; set; }
    public Detalhe[] detalhes { get; set; }
    public Subdetalhes1 subdetalhes { get; set; }
    public int quantidade { get; set; }
}

public class Subdetalhes1
{
}

public class Detalhe
{
    public int codigo { get; set; }
    public string descricao { get; set; }
    public string data { get; set; }
    public string ticket { get; set; }
    public bool mensal { get; set; }
    public Terminal terminal { get; set; }
    public Setor setor { get; set; }
    public Tipoveiculo tipoVeiculo { get; set; }
    public Tiposervico tipoServico { get; set; }
}

public class Terminal
{
    public int codigo { get; set; }
    public string tipo { get; set; }
}

public class Setor
{
    public string descricao { get; set; }
}

public class Tipoveiculo
{
    public string descricao { get; set; }
}

public class Tiposervico
{
    public string descricao { get; set; }
}

public class Caminhao
{
    public string descricao { get; set; }
    public object[] detalhes { get; set; }
    public Subdetalhes2 subdetalhes { get; set; }
    public int quantidade { get; set; }
}

public class Subdetalhes2
{
}

public class Moto
{
    public string descricao { get; set; }
    public object[] detalhes { get; set; }
    public Subdetalhes3 subdetalhes { get; set; }
    public int quantidade { get; set; }
}

public class Subdetalhes3
{
}

Browser other questions tagged

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