【问题标题】:Deserializing nested json into vb.net class using json.net使用 json.net 将嵌套的 json 反序列化为 vb.net 类
【发布时间】:2023-03-22 20:32:01
【问题描述】:

我有一些 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、一个字典和上面的列表,但没有任何效果。

【问题讨论】:

  • 仅供参考,您可以使用jsonlint.com 漂亮地打印 JSON。

标签: json vb.net json.net json-deserialization


【解决方案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 属性。它仅用于二进制序列化。

【讨论】:

  • 谢谢你们……天哪……这是我的愚蠢错误……所以……它现在可以工作了,但是……达林……当我做propertyname时,视觉工作室不会编译.我真的很喜欢这个,因为返回的 json 内容之一称为错误,当然 Visual Studio 不会让我拥有一个名为 hehe 的类属性。
【解决方案2】:

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

Public Property Countries as List(Of DPDCountry)

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

Public Property Country as List(Of DPDCountry)

【讨论】:

    猜你喜欢
    • 2016-01-17
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多