使用 json.net 将嵌套 json 反序列化为 vb.net class

Deserializing nested json into vb.net class using json.net

我有一些 json 正试图反序列化为一些 vb.net 对象

这里是 类

<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

这里是 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
            }
        ]
    }
}

这是反序列化它的代码

Dim oResponseData As DPDMultiCountryDataRequest = _
    JsonConvert.DeserializeObject(Of DPDMultiCountryDataRequest)(tmp)

国家列表总是空的。级别高的没问题。我还有一个例程,可以获取一个国家/地区的信息,效果很好。是多个国家害了我。

我试过一个数组、一个 iList、一个字典和上面的列表,但没有任何效果。

您的 json 包含名为 country 的 属性,但您的对象包含名为 Countries 的 属性:

Public Property Countries as List(Of DPDCountry)

反序列化 json 时,名称绝对重要。将名称更新为 Country:

Public Property Country as List(Of DPDCountry)

属性必须叫Country,而不是Countries

<Serializable()>
Public Class DPDMultiCountryDataResponse
    Public Property Country as List(Of DPDCountry)

或者,您可以使用 JsonProperty 属性:

<Serializable()>
Public Class DPDMultiCountryDataResponse
    <JsonProperty(PropertyName = "Country")>
    Public Property Countries as List(Of DPDCountry)

另请记住,不需要 Serializable 属性。它仅用于二进制序列化。