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

时间:2015-02-22 16:54:57

标签: 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,一个字典和上面的列表,但没有任何作用。

让我发疯了呵呵。

再次感谢您的帮助

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

Public Property Countries as List(Of DPDCountry)

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

Public Property Country as List(Of DPDCountry)