2

Segue json:

[  
   {  
      "id":"BKA-EU8IED8ZD21Q",
      "agencyNumber":"1",
      "accountNumber":"1",
      "holder":{  
         "thirdParty":false,
         "taxDocument":{  
            "number":"783.121.360-02",
            "type":"CPF"
         },
         "fullname":"Leandro da silva"
      },
      "status":"NOT_VERIFIED",
      "createdAt":"2018-08-23T10:59:01.000-03:00",
      "accountCheckNumber":"1",
      "_links":{  
         "self":{  
            "href":""
         }
      },
      "bankName":"BANCO DO BRASIL S.A.",
      "type":"SAVING",
      "agencyCheckNumber":"1",
      "bankNumber":"001"
   },
   {  
      "id":"BKA-3KF1C4O7F247",
      "agencyNumber":"1",
      "accountNumber":"1",
      "holder":{  
         "thirdParty":false,
         "taxDocument":{  
            "number":"783.121.360-02",
            "type":"CPF"
         },
         "fullname":"Leandro da silva"
      },
      "status":"NOT_VERIFIED",
      "createdAt":"2018-08-24T14:06:25.000-03:00",
      "accountCheckNumber":"1",
      "_links":{  
         "self":{  
            "href":""
         }
      },
      "bankName":"BANCO BRADESCO S.A.",
      "type":"CHECKING",
      "agencyCheckNumber":"1",
      "bankNumber":"237"
   },
   {  
      "id":"BKA-ZFYCD7NIPK9H",
      "agencyNumber":"1",
      "accountNumber":"1",
      "holder":{  
         "thirdParty":false,
         "taxDocument":{  
            "number":"783.121.360-02",
            "type":"CPF"
         },
         "fullname":"Leandro da silva"
      },
      "status":"NOT_VERIFIED",
      "createdAt":"2018-08-24T15:06:52.000-03:00",
      "accountCheckNumber":"1",
      "_links":{  
         "self":{  
            "href":""
         }
      },
      "bankName":"BANCO BRADESCO S.A.",
      "type":"CHECKING",
      "agencyCheckNumber":"1",
      "bankNumber":"237"
   },
   {  
      "id":"BKA-0E7J1UH937M1",
      "agencyNumber":"1",
      "accountNumber":"1",
      "holder":{  
         "thirdParty":false,
         "taxDocument":{  
            "number":"783.121.360-02",
            "type":"CPF"
         },
         "fullname":"Leandro da silva"
      },
      "status":"NOT_VERIFIED",
      "createdAt":"2018-08-24T15:08:01.000-03:00",
      "accountCheckNumber":"1",
      "_links":{  
         "self":{  
            "href":""
         }
      },
      "bankName":"BANCO BRADESCO S.A.",
      "type":"CHECKING",
      "agencyCheckNumber":"1",
      "bankNumber":"237"
   },
   {  
      "id":"BKA-OIGOJHQB82FS",
      "agencyNumber":"1",
      "accountNumber":"1",
      "holder":{  
         "thirdParty":false,
         "taxDocument":{  
            "number":"783.121.360-02",
            "type":"CPF"
         },
         "fullname":"Leandro da silva"
      },
      "status":"NOT_VERIFIED",
      "createdAt":"2018-08-24T16:00:27.000-03:00",
      "accountCheckNumber":"1",
      "_links":{  
         "self":{  
            "href":""
         }
      },
      "bankName":"BANCO BRADESCO S.A.",
      "type":"CHECKING",
      "agencyCheckNumber":"1",
      "bankNumber":"237"
   },
   {  
      "id":"BKA-IWYY1DPM1S68",
      "agencyNumber":"12345",
      "accountNumber":"12345678",
      "holder":{  
         "thirdParty":false,
         "taxDocument":{  
            "number":"783.121.360-02",
            "type":"CPF"
         },
         "fullname":"Leandro da silva"
      },
      "status":"NOT_VERIFIED",
      "createdAt":"2018-08-25T16:13:51.000-03:00",
      "accountCheckNumber":"7",
      "_links":{  
         "self":{  
            "href":""
         }
      },
      "bankName":"BANCO BRADESCO S.A.",
      "type":"CHECKING",
      "agencyCheckNumber":"0",
      "bankNumber":"237"
   }
]

Segue as classes:

public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }

    public class Class1
    {
        public string id { get; set; }
        public string agencyNumber { get; set; }
        public string accountNumber { get; set; }
        public Holder holder { get; set; }
        public string status { get; set; }
        public DateTime createdAt { get; set; }
        public string accountCheckNumber { get; set; }
        public _Links _links { get; set; }
        public string bankName { get; set; }
        public string type { get; set; }
        public string agencyCheckNumber { get; set; }
        public string bankNumber { get; set; }
    }

    public class Holder
    {
        public bool thirdParty { get; set; }
        public Taxdocument taxDocument { get; set; }
        public string fullname { get; set; }
    }

    public class Taxdocument
    {
        public string number { get; set; }
        public string type { get; set; }
    }

    public class _Links
    {
        public Self self { get; set; }
    }

    public class Self
    {
        public string href { get; set; }
    }

Segue a lógica para desserializar:

var result = JsonConvert.DeserializeObject<Rootobject[]>(json);

Cada array tem a sua propriedade Property1 e essa propriedade é sempre nulo.

Como posso resolver isso ?

2
  • 1
    O json informado parece ser um Array do objeto Classe1. Tente o seguinte: var result = JsonConvert.DeserializeObject<Classe1[]>(json); Commented 29/08/2018 às 22:37
  • @RobertoBraga faz sentido Commented 29/08/2018 às 22:40

1 Resposta 1

2

Conforme dito no comentário do Roberto o Json é um Array, entretanto, não deveria ter essa public Class1[] Property1 { get; set; }, deixe sua classe assim:

public class TaxDocument
{
    public string number { get; set; }
    public string type { get; set; }
}

public class Holder
{
    public bool thirdParty { get; set; }
    public TaxDocument taxDocument { get; set; }
    public string fullname { get; set; }
}

public class Self
{
    public string href { get; set; }
}

public class Links
{
    public Self self { get; set; }
}

public class RootObject
{
    public string id { get; set; }
    public string agencyNumber { get; set; }
    public string accountNumber { get; set; }
    public Holder holder { get; set; }
    public string status { get; set; }
    public DateTime createdAt { get; set; }
    public string accountCheckNumber { get; set; }
    public Links _links { get; set; }
    public string bankName { get; set; }
    public string type { get; set; }
    public string agencyCheckNumber { get; set; }
    public string bankNumber { get; set; }
}

E para fazer o Deserialize:

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

Você pode usar o site http://json2csharp.com/ para gerar as respectivas classes de um Json

Dica de leitura: Qual é a diferença entre chaves “{ }” e colchetes “[ ]”?

1
  • Usei a opção de menu "Colar especial como json", foi aí que falhou :/ Commented 29/08/2018 às 23:35

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .