Jsonconvert.Deserializeobject returns null

Asked

Viewed 156 times

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"
   }
]

Follows the 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; }
    }

Follow the logic to deserialize:

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

Each array has its own property Property1 and this property is always null.

How can I fix this ?

  • 1

    The informed json appears to be an object array Classe1. Try the following: var result = JsonConvert.DeserializeObject<Classe1[]>(json);

  • @Robertobraga makes sense

1 answer

2


As stated in the commentary of Roberto the Json is a Array, however, should not have this public Class1[] Property1 { get; set; }, leave your class like this:

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; }
}

And to do the Deserialize:

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

You can use the website http://json2csharp.com/ to generate the respective classes of a Json

Reading tip: What is the difference between keys "{ }" and brackets "[ ]"?

  • I used the "Paste special as json" menu option, that’s where it failed :/

Browser other questions tagged

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