Error Deserialize Json in Model C # [duplicate]

2

Friends, I'm getting error to do the deserialize of the following JSON:

{
    "Comanda": [
        {
            "status": "Produzido (Codigo 3)",
            "estabelecimento_id": 18,
            "cliente_nome": "Marcos Manfrin",
            "estabelecimento": "Aquaria",
            "formas_de_pagamentos": [
                {
                    "valor_final": "69.00",
                    "valor_total": "120.00",
                    "tipo": "Dinheiro"
                },
                {
                    "valor_final": "69.00",
                    "valor_total": "120.00",
                    "tipo": "Débito Visa"
                }
            ],
            "apto": "402",
            "pedido_id": 35370,
            "data_venda": "07/06/2017",
            "items": [
                {
                    "quantidade": 4,
                    "nome": "FOTO EM ARQUIVO DIGITAL",
                    "valor_custo": "1.00",
                    "observacoes": "Mandar para esse e-mail: [email protected]",
                    "descricao": "",
                    "produto_id": 78693,
                    "valor_venda": "20.00"
                },
                {
                    "quantidade": 2,
                    "nome": "FOTO EM ARQUIVO DIGITAL",
                    "valor_custo": "1.00",
                    "observacoes": "Mandar para esse e-mail: [email protected]",
                    "descricao": "",
                    "produto_id": 78694,
                    "valor_venda": "20.00"
                }
            ],
            "comanda": "2103",
            "fotografo_id": 36,
            "fotografo": "Jaasiel Oliveira",
            "codigo": "C819F0"
        }
    ]
}

In the following template:

public class FormasDePagamento
{
    public string valor_final { get; set; }
    public string valor_total { get; set; }
    public string tipo { get; set; }
}

public class Item
{
    public int quantidade { get; set; }
    public string nome { get; set; }
    public string valor_custo { get; set; }
    public string observacoes { get; set; }
    public string descricao { get; set; }
    public int produto_id { get; set; }
    public string valor_venda { get; set; }
}

public class Comanda
{
    public string status { get; set; }
    public int estabelecimento_id { get; set; }
    public string cliente_nome { get; set; }
    public string estabelecimento { get; set; }
    public List<FormasDePagamento> formas_de_pagamentos { get; set; }
    public string apto { get; set; }
    public int pedido_id { get; set; }
    public string data_venda { get; set; }
    public List<Item> items { get; set; }
    public string comanda { get; set; }
    public int fotografo_id { get; set; }
    public string fotografo { get; set; }
    public string codigo { get; set; }
}

I'm using this code:

var PedidosApi = JsonConvert.DeserializeObject<List<Models.Comanda>>(json);

But I get the following error:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [. Path '[0].Comanda', line 1, position 14.'

Somebody give me a light, please.

    
asked by anonymous 22.08.2017 / 16:57

4 answers

3

Create this class

public class Json
{
    public List<Models.Comanda> Comanda { get; set; }
}

Use it as the type parameter of the DeserializeObject method

var PedidosApi = JsonConvert.DeserializeObject<Json>(json);
    
22.08.2017 / 18:40
3

If you need a list of commands, you have to create the root class that encapsulates your Json's list of commands.

public class Rootobject
{
        public List<Comanda> Comandas { get; set; }
}

The call is:

var pedidosApi = JsonConvert.DeserializeObject<Rootobject>(json);

And finally I renamed Json's command array to commands.

 "Comandas": [...

You can use [JsonProperty(PropertyName = "bar")] to organize naming issues between C # classes and Json properties.

    
22.08.2017 / 18:58
1

This happens because your Json is mounted differently than your conversion

What your json should have is a list of objects of type Comanda . Below is the correct Json:

[
    {
        "status": "Produzido (Codigo 3)",
        "estabelecimento_id": 18,
        "cliente_nome": "Marcos Manfrin",
        "estabelecimento": "Aquaria",
        "formas_de_pagamentos": [
            {
                "valor_final": "69.00",
                "valor_total": "120.00",
                "tipo": "Dinheiro"
            },
            {
                "valor_final": "69.00",
                "valor_total": "120.00",
                "tipo": "Débito Visa"
            }
        ],
        "apto": "402",
        "pedido_id": 35370,
        "data_venda": "07/06/2017",
        "items": [
            {
                "quantidade": 4,
                "nome": "FOTO EM ARQUIVO DIGITAL",
                "valor_custo": "1.00",
                "observacoes": "Mandar para esse e-mail: [email protected]",
                "descricao": "",
                "produto_id": 78693,
                "valor_venda": "20.00"
            },
            {
                "quantidade": 2,
                "nome": "FOTO EM ARQUIVO DIGITAL",
                "valor_custo": "1.00",
                "observacoes": "Mandar para esse e-mail: [email protected]",
                "descricao": "",
                "produto_id": 78694,
                "valor_venda": "20.00"
            }
        ],
        "comanda": "2103",
        "fotografo_id": 36,
        "fotografo": "Jaasiel Oliveira",
        "codigo": "C819F0"
    }
]

With this json you can deserialize to List ();

See working at .NetFiddle

    
22.08.2017 / 18:39
0

To convert Json I made the following adjustments: - Remove the hierarchy that contains the command object - Remove the keys within the command hierarchy, as there is only one object - Remove the Deserialization List

Follow Json:

{
    "status": "Produzido (Codigo 3)",
    "estabelecimento_id": 18,
    "cliente_nome": "Marcos Manfrin",
    "estabelecimento": "Aquaria",
    "formas_de_pagamentos": [
        {
            "valor_final": "69.00",
            "valor_total": "120.00",
            "tipo": "Dinheiro"
        },
        {
            "valor_final": "69.00",
            "valor_total": "120.00",
            "tipo": "Débito Visa"
        }
    ],
    "apto": "402",
    "pedido_id": 35370,
    "data_venda": "07/06/2017",
    "items": [
      {
            "quantidade": 4,
            "nome": "FOTO EM ARQUIVO DIGITAL",
            "valor_custo": "1.00",
            "observacoes": "Mandar para esse e-mail: [email protected]",
            "descricao": "",
            "produto_id": 78693,
            "valor_venda": "20.00"
        },
        {
            "quantidade": 2,
            "nome": "FOTO EM ARQUIVO DIGITAL",
            "valor_custo": "1.00",
            "observacoes": "Mandar para esse e-mail: [email protected]",
            "descricao": "",
            "produto_id": 78694,
            "valor_venda": "20.00"
        }
    ],
    "comanda": "2103",
    "fotografo_id": 36,
    "fotografo": "Jaasiel Oliveira",
    "codigo": "C819F0"
}

Follow the command

var PedidosApi = JsonConvert.DeserializeObject<Models.Comanda>(json);
    
22.08.2017 / 18:50