0

I'm trying to deserialize a JSON, but all properties of my object come null.

My JSON:

{"List":{"item":[{"id":"47861","nome":"2,4-D AMINA 840 SL","codMapa":"5002","fabricante":"ALBAUGH AGRO BRASIL LTDA","embalagem":"BALDE","tipoEmbalagem":"AÇO","volume":"20.0000","unidade":"LITROS"},{"id":"47863","nome":"2,4-D AMINA 840 SL","codMapa":"5002","fabricante":"ALBAUGH AGRO BRASIL LTDA","embalagem":"BOMBONA","tipoEmbalagem":"PLASTICO","volume":"5.0000","unidade":"LITROS"},{"id":"47865","nome":"2,4-D AMINA 840 SL","codMapa":"5002","fabricante":"ALBAUGH AGRO BRASIL LTDA","embalagem":"BOMBONA","tipoEmbalagem":"PLASTICO","volume":"10.0000","unidade":"LITROS"},{"id":"47867","nome":"2,4-D AMINA 840 SL","codMapa":"5002","fabricante":"ALBAUGH AGRO BRASIL LTDA","embalagem":"BOMBONA","tipoEmbalagem":"PLASTICO","volume":"20.0000","unidade":"LITROS"},{"id":"47869","nome":"2,4-D AMINA 840 SL","codMapa":"5002","fabricante":"ALBAUGH AGRO BRASIL LTDA","embalagem":"CAIXA","tipoEmbalagem":"PAPELÃO","volume":"20.0000","unidade":"LITROS"},{"id":"47871","nome":"2,4-D AMINA 840 SL","codMapa":"5002","fabricante":"ALBAUGH AGRO BRASIL LTDA","embalagem":"FRASCO","tipoEmbalagem":"PLASTICO","volume":"1.0000","unidade":"LITROS"},{"id":"47873","nome":"2,4-D AMINA 840 SL","codMapa":"5002","fabricante":"ALBAUGH AGRO BRASIL LTDA","embalagem":"TAMBOR","tipoEmbalagem":"AÇO","volume":"200.0000","unidade":"LITROS"}]}}


My class:

    public class ListaProdutosAutorizados
    {
        public List<ProdutoAutorizado> ProdutosAutorizados { get; set; }
    }

    public class ProdutoAutorizado
    {
        public int id { get; set; }
        public string nome { get; set; }
        public int? codMapa { get; set; }
        public string fabricante { get; set; }
        public string embalagem { get; set; }
        public string tipoEmbalagem { get; set; }
        public decimal? volume { get; set; }
        public string unidade { get; set; }
    }

And my Code:

var lResposta = JsonConvert.DeserializeObject<ListaProdutosAutorizados>(stringJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

Daniel Marín
  • 1,369
  • 14
  • 36
dehoziiN
  • 5
  • 2
  • 1
    C# classes of yours do not match with the JSON Structure... user http://json2csharp.com/ to generate C# classes for your JSON. – Chetan Sep 16 '19 at 13:01

2 Answers2

2

Your JSON shoud look like this to match your C# classes:

[
  {
    "id": 47861,
    "nome": "2,4-D AMINA 840 SL",
    "codMapa": 5002,
    "fabricante": "ALBAUGH AGRO BRASIL LTDA",
    "embalagem": "BALDE",
    "tipoEmbalagem": "AÇO",
    "volume": 20.0000,
    "unidade": "LITROS"
  },
  {
    "id": 47863,
    "nome": "2,4-D AMINA 840 SL",
    "codMapa": 5002,
    "fabricante": "ALBAUGH AGRO BRASIL LTDA",
    "embalagem": "BOMBONA",
    "tipoEmbalagem": "PLASTICO",
    "volume": 5.0000,
    "unidade": "LITROS"
  },
  {
    "id": 47865,
    "nome": "2,4-D AMINA 840 SL",
    "codMapa": 5002,
    "fabricante": "ALBAUGH AGRO BRASIL LTDA",
    "embalagem": "BOMBONA",
    "tipoEmbalagem": "PLASTICO",
    "volume": 10.0000,
    "unidade": "LITROS"
  },
  {
    "id": 47867,
    "nome": "2,4-D AMINA 840 SL",
    "codMapa": 5002,
    "fabricante": "ALBAUGH AGRO BRASIL LTDA",
    "embalagem": "BOMBONA",
    "tipoEmbalagem": "PLASTICO",
    "volume": 20.0000,
    "unidade": "LITROS"
  },
  {
    "id": 47869,
    "nome": "2,4-D AMINA 840 SL",
    "codMapa": 5002,
    "fabricante": "ALBAUGH AGRO BRASIL LTDA",
    "embalagem": "CAIXA",
    "tipoEmbalagem": "PAPELÃO",
    "volume": 20.0000,
    "unidade": "LITROS"
  },
  {
    "id": 47871,
    "nome": "2,4-D AMINA 840 SL",
    "codMapa": 5002,
    "fabricante": "ALBAUGH AGRO BRASIL LTDA",
    "embalagem": "FRASCO",
    "tipoEmbalagem": "PLASTICO",
    "volume": 1.0000,
    "unidade": "LITROS"
  },
  {
    "id": 47873,
    "nome": "2,4-D AMINA 840 SL",
    "codMapa": 5002,
    "fabricante": "ALBAUGH AGRO BRASIL LTDA",
    "embalagem": "TAMBOR",
    "tipoEmbalagem": "AÇO",
    "volume": 200.0000,
    "unidade": "LITROS"
  }
]

Note that your id, codMapa and volume properties can get unserialized wrongly as their type in your original JSON is string and not decimal or int.

If you deserialize this new JSON into a List<ProdutoAutorizado> it should work.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
  • Nice point Corentin, and for Daniel, that's always good take a look into de docs > https://www.newtonsoft.com/json/help/html/DeserializeObject.htm – Fábio BC Souza Sep 16 '19 at 13:51
0

You need same property names on the json file. In your example you have List then there must be a property in the C# class called List and same thing for the item as well.

The other way to match them is using JsonProperty attribute.

Check this question for more info.

darcane
  • 473
  • 3
  • 14