I have some json which I am trying to deserialise into some vb.net objects
Here are the classes
<Serializable()>
Public Class DPDError
Public Property errorAction As String
Public Property errorCode As String
Public Property errorMessage As String
Public Property errorObj As String
Public Property errorType As String
End Class
<Serializable()>
Public Class DPDCountry
Public Property countryCode As String
Public Property countryName As String
Public Property isoCode As String
Public Property isEUCountry As Boolean
Public Property isLiabilityAllowed As Boolean
Public Property liabilityMax As Integer
Public Property isPostcodeRequired As Boolean
End Class
'----- USED TO GET ALL COUNTRY INFO
<Serializable()>
Public Class DPDMultiCountryDataResponse
Public Property Countries as List(Of DPDCountry)
End Class
<Serializable()>
Public Class DPDMultiCountryDataRequest
Public Property DpdError As DPDError
Public Property Data As DPDMultiCountryDataResponse
End Class
Here is the JSON:
{
"data": {
"country": [
{
"countryCode": "UY",
"countryName": "Uruguay",
"isoCode": "858",
"isEUCountry": false,
"isLiabilityAllowed": true,
"liabilityMax": 15000,
"isPostcodeRequired": true
},
{
"countryCode": "US",
"countryName": "Usa",
"isoCode": "840",
"isEUCountry": false,
"isLiabilityAllowed": true,
"liabilityMax": 15000,
"isPostcodeRequired": true
},
{
"countryCode": "VU",
"countryName": "Vanuatu",
"isoCode": "548",
"isEUCountry": false,
"isLiabilityAllowed": true,
"liabilityMax": 15000,
"isPostcodeRequired": true
},
{
"countryCode": "VN",
"countryName": "Vietnam",
"isoCode": "704",
"isEUCountry": false,
"isLiabilityAllowed": true,
"liabilityMax": 15000,
"isPostcodeRequired": true
}
]
}
}
Here is the code to deserialise it
Dim oResponseData As DPDMultiCountryDataRequest = _
JsonConvert.DeserializeObject(Of DPDMultiCountryDataRequest)(tmp)
The countries list is always nothing. The higher level ones are fine. I also have a routine which gets ONE country info which works fine. Its the multiple countries that is killing me.
I have tried an array, an iList, a dictionary and the list as above and nothing works.
The property must be called Country
, not Countries
:
<Serializable()>
Public Class DPDMultiCountryDataResponse
Public Property Country as List(Of DPDCountry)
Alternatively you could use the JsonProperty attribute:
<Serializable()>
Public Class DPDMultiCountryDataResponse
<JsonProperty(PropertyName = "Country")>
Public Property Countries as List(Of DPDCountry)
Also bear in mind that the Serializable
attribute is not needed. It is used only for binary serialization.