Desirializing complex json

Asked

Viewed 170 times

2

I’m new around here and I’d like your help, I need to deserialize a json, to use in an Xamarin Forms application and I’m having difficulties, 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 within a list to do Binding in the properties, method I’m using, follows

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

Ao executar meu código tenho isto de retorno

When executing my code I have this return

  • 1

    Swap the image for text, this will make it easier for someone to help you. Whenever possible, avoid images

  • Example: https://answall.com/questions/168348/serializando-e-desserializando-objetos-json-com-c

  • Example: https://answall.com/questions/219560/deserialize-json-com-restsharp-e-com-javascriptserializer/219627#219627

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 package Newtonsoft.Json - (Json.NET) and with that installation execute the following code:

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

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

Other examples:

  • the problem with the above code is that I get an exception of type, Unhandled Exception: Newtonsoft.Json.Jsonserializationexception: Cannot deserialize the Current JSON Object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Estrelaapp.Model.Catalogo]'because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

  • @Fláviojune I did according to the question I think I missed something. Ask the question the full example

  • @Fláviojune is some field in json with problem. Note this

  • Good evening @Virgilio Novic, I did according to your example has not yet worked in the variable "json" I have the data but I can not deserialize it, the variable "Catalogos is null"

  • Play full json @Fláviojune

  • The json is this same what changes is only the amount of objects contained in it, but the structure is this

  • @FLÁVIOJUNE YOUR CLASS CATALOGO PLEASE

  • @Fláviojune you keep doing wrong by the way.

  • @Fláviojune you made a different class from my answer?

Show 4 more comments

Browser other questions tagged

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