Deserializing complex json

2

I'm new here and would like help you, I need to deserialize a json, to use in a xamarin forms application and am having trouble, how to proceed in C #? follow the json below

{
  "RECORDS": [{
      "Codigo": "33338",
      "Descricao": "MOLA WIL 50-1 - TRAS RURAL - 57\/64",
      "PrecoVenda": 74.75,
      "Referencia": "WIL 50-1",
      "Grupo": "002",
      "GrupoDesc": "MAM WILLYS",
      "Categoria": 3,
      "CategoriaDesc": "Material Acabado - Molas",
      "Classe": "001",
      "ClasseDesc": "MOLAS",
      "NCM": "7320.10.00"
    },
    {
      "Codigo": "33339",
      "Descricao": "MOLA WIL 50-2 - TRAS RURAL - 57\/64",
      "PrecoVenda": 69,
      "Referencia": "WIL 50-2",
      "Grupo": "002",
      "GrupoDesc": "MAM WILLYS",
      "Categoria": 3,
      "CategoriaDesc": "Material Acabado - Molas",
      "Classe": "001",
      "ClasseDesc": "MOLAS",
      "NCM": "7320.10.00"
    }
  ]
}

I need to deserialize and play inside a the list to do Binding in the properties, method that I am using, follows

public async void Ler()
    {
       var json = await LerArquivo.CarregarArquivo("Catalogo.json");
       var Catalogos = JsonConvert.DeserializeObject<List<Catalogo>>(json);
    }

When running my code I have this comeback

    
asked by anonymous 14.10.2018 / 01:07

1 answer

0

Create two classes with the following code:

public class Rootobject
{
    public RECORD[] RECORDS { get; set; }
}

public class RECORD
{
    public string Codigo { get; set; }
    public string Descricao { get; set; }
    public string PrecoVenda { get; set; }
    public string Referencia { get; set; }
    public string Grupo { get; set; }
    public string GrupoDesc { get; set; }
    public string Categoria { get; set; }
    public string CategoriaDesc { get; set; }
    public string Classe { get; set; }
    public string ClasseDesc { get; set; }
    public string NCM { get; set; }
}

Now install the Newtonsoft.Json- (Json.NET) package and run the following code:

var result = Newtonsoft
            .Json
            .JsonConvert
            .DeserializeObject<Rootobject>(File.ReadAllText("./data.json"));

In this case the result variable will contain the list you need. You can also make an adjustment in the PrecoVenda field by changing its type to decimal .

Other examples :

14.10.2018 / 01:56