How to convert this Json to Objects in C#

Asked

Viewed 400 times

1

How to convert this JSON to a C object#?

{
   "Maquinas":{
      "Maquina1":[
         {
            "comando":"Get",
            "enderecoip":"192.168.254.221",
            "port":"3000",
            "usuario":"teste",
            "Senha":"123456"
         },
         {
            "comando":"sendFuncionarios",
            "enderecoip":"192.168.140.221",
            "port":"3000",
            "usuario":"teste fabrica",
            "Senha":"111111",
            "funcionariosHA":[
               {
                  "nome":"Daril Mont",
                  "pis":"1234567891",
                  "verdigital":false,
                  "codigo":30
               },
               {
                  "nome":"Mont Daril",
                  "pis":"62633975303",
                  "verdigital":true,
                  "codigo":31
               }
            ]
         },
         {
            "comando":"sendEmpresa",
            "enderecoip":"192.168.140.221",
            "port":"3000",
            "usuario":"fabrica",
            "Senha":"123456",
            "EmpresaHexaAdv":[
               {
                  "razaoSocial":"Oficina de Computadores",
                  "tipoCadastro":"CNPJ",
                  "CPF":"",
                  "CNPJ":"20330852471",
                  "endereco":"R. Mariana, 265",
                  "CEI":""
               }
            ]
         }
      ],
      "Maquina2":[
         {
            "comando":"Get",
            "enderecoip":"192.168.210.223",
            "port":"51000",
            "cpf":"13556285630"
         },
         {
            "comando":"sendFuncionarios",
            "enderecoip":"192.168.210.223",
            "port":"51000",
            "cpf":"13556285630",
            "funcionariosInnerREP":[
               {
                  "nome":"Dorval Miguel",
                  "pis":"1234567891",
                  "verdigital":false,
                  "codigo":30,
                  "senha":"0030"
               },
               {
                  "nome":"Leticia do Carmo",
                  "pis":"62633975303",
                  "verdigital":true,
                  "codigo":31,
                  "senha":"0031"
               }
            ]
         },
         {
            "comando":"sendEmpresa",
            "enderecoip":"192.168.210.223",
            "port":"51000",
            "cpf":"13556285630",
            "EmpresaInnerREP":[
               {
                  "razaoSocial":"Oficina de Computadores",
                  "tipoCadastro":"CNPJ",
                  "CPF":"",
                  "CNPJ":"20330852471",
                  "endereco":"Rua abacate 25",
                  "CEI":""
               }
            ]
         }
      ]
   }
}
  • 1

    your question has been answered here https://answall.com/questions/706/comoraconverter-uma-resposta-em-json-para-um-objeto-em-c

  • @Victorwillian Maquinas will only have two attributes Maquina1 and Maquina2 if this number may vary you will have to change your Json

1 answer

0


An easy way to create a class that represents a Json (in Dotnet 4.5 or higher) is to copy Json content to the clipboard, create a new class, and use the menu command Edit > Paste Special > Paste JSON as Classes. The IDE will automatically create one or more classes that represent your JSON structure.

Having this class you can easily Deserialize a JSON string in an instance of this class using JSON.NET. See an Example:

public static MeuObjeto Deserializar(string json)
    {
        return JsonConvert.DeserializeObject<MeuObjeto>(json);
    }

Browser other questions tagged

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