2

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.

1
  • Just FYI, you can use jsonlint.com to pretty-print JSON. Commented Feb 22, 2015 at 17:00

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you guys ... OMG ... this was my jackass mistake... so... it now works but ... Darin... when I do the propertyname visual studio wont compile. I really like this as one of the json things returned is called error and of course visual studio wont let me have a class property with that name hehe.
0

Your json contains property named country, but your object contains property that is called Countries:

Public Property Countries as List(Of DPDCountry)

When deserializing json names definitely matter. Update the name to Country:

Public Property Country as List(Of DPDCountry)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.