使用json.net将嵌套的json反序列化到vb.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、字典和上面的列表,但都不起作用。

属性必须被称为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属性。它仅用于二进制序列化。

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

Public Property Countries as List(Of DPDCountry)

在反序列化json时,名称肯定很重要。将名称更新为Country:

Public Property Country as List(Of DPDCountry)

相关内容

  • 没有找到相关文章

最新更新