【问题标题】:JSON deserialize with array and non-ArrayJSON用数组和非数组反序列化
【发布时间】:2014-11-14 10:31:30
【问题描述】:

我有一个 JSON 字符串。有这样的部分

    "Result": {
      "AdditionalInfo": {
        "SubCategoryID": "978",
        "SellerPartNumber": "04-VO7T-14PP",
        "ManufacturerPartNumberOrISBN": "04-VO7T-14PP",
        "UPC": null
      },
      "ErrorList": {
        "ErrorDescription": [
          {
            "#cdata-section": "Error(s). Item not created."
          },
          {
            "#cdata-section": "Error:[Item does not exist. Please create a new list.]"
          }
        ]
      }
    }

但是,有时该部分也是一个数组。 喜欢

    "Result": [
      {
        "AdditionalInfo": {
          "SubCategoryID": "1512",
          "SellerPartNumber": "TY-SZT1-358V",
          "ManufacturerPartNumberOrISBN": "TY-SZT1-358V",
          "UPC": null
        },
        "ErrorList": {
          "ErrorDescription": [
            {
              "cdata-section": "Error(s). Item not created."
            },
            {
              "cdata-section": "CufflinkMaterial - Property: [CufflinkMaterial] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
            },
            {
              "cdata-section": "CufflinkType - Property: [CufflinkType] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
            }
          ]
        }
      },
      {
        "AdditionalInfo": {
          "SubCategoryID": "1512",
          "SellerPartNumber": "UF-T05C-T6XG",
          "ManufacturerPartNumberOrISBN": "UF-T05C-T6XG",
          "UPC": null
        },
        "ErrorList": {
          "ErrorDescription": [
            {
              "cdata-section": "Error(s). Item not created."
            },
            {
              "cdata-section": "CufflinkMaterial - Property: [CufflinkMaterial] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
            },
            {
              "cdata-section": "CufflinkType - Property: [CufflinkType] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
            }
          ]
        }
      },
      {
        "AdditionalInfo": {
          "SubCategoryID": "1512",
          "SellerPartNumber": "5B-1137-WT3O",
          "ManufacturerPartNumberOrISBN": "5B-1137-WT3O",
          "UPC": null
        },
        "ErrorList": {
          "ErrorDescription": [
            {
              "cdata-section": "Error(s). Item not created."
            },
            {
              "cdata-section": "CufflinkMaterial - Property: [CufflinkMaterial] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
            },
            {
              "cdata-section": "CufflinkType - Property: [CufflinkType] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
            }
          ]
        }
      }

          ]

是否有一种通用方法可以在不定义不同对象的情况下对两个 JSON 进行反序列化?所以像,我定义了一个Result对象数组,当有单个实体时,它会在对象数组中创建一个索引,当有多个时,它可能会在数组中创建多个索引。

最终目标是使用 object 对象并使用它来解析单个 Result 对象或多个。

这可能吗? 谢谢 萨默斯

【问题讨论】:

  • 您使用的是哪个序列化程序?使用Json.Net 绝对可以使用JsonConverter。有关 C# 中的解决方案,请参阅 this question。你应该可以把它翻译成 vb.net。

标签: arrays json vb.net


【解决方案1】:

感谢布赖恩·罗杰斯

答案在您的链接中。

How to handle both a single item and an array for the same property using JSON.net

我使用此代码将其转换为 VB.NET。

Class SingleOrArrayConverter(Of T)
    Inherits Newtonsoft.Json.JsonConverter
    Public Overrides Function CanConvert(ByVal objectType As Type) As Boolean
        'Return (objectType = GetType(List(Of T)))
        Return objectType.Equals(GetType(Generic.List(Of T)))
    End Function

    Public Overrides Function ReadJson(ByVal reader As Newtonsoft.Json.JsonReader, ByVal objectType As Type, ByVal existingValue As Object, ByVal serializer As Newtonsoft.Json.JsonSerializer) As Object
        Dim token As Newtonsoft.Json.Linq.JToken = Newtonsoft.Json.Linq.JToken.Load(reader)
        If token.Type = Newtonsoft.Json.Linq.JTokenType.Array Then
            Return token.ToObject(Of Generic.List(Of T))()
        End If
        Dim list As New Generic.List(Of T)
        list.Add(token.ToObject(Of T)())
        Return list
    End Function

    Public Overrides ReadOnly Property CanWrite() As Boolean
        Get
            Return False
        End Get
    End Property

    Public Overrides Sub WriteJson(ByVal writer As Newtonsoft.Json.JsonWriter, ByVal value As Object, ByVal serializer As Newtonsoft.Json.JsonSerializer)
        Throw New NotImplementedException()
    End Sub
End Class

并将此属性应用于我的数据定义。

    <Newtonsoft.Json.JsonProperty("Result")> _
    <Newtonsoft.Json.JsonConverter(GetType(Utilities.JSONUtilities.SingleOrArrayConverter(Of FeedResultType)))> _
    Public Property Result() As Generic.List(Of FeedResultType)
        Get
            Return m_Result
        End Get
        Set(ByVal value As Generic.List(Of FeedResultType))
            m_Result = value
        End Set
    End Property

现在一切都很好。 谢谢 萨默斯

是的,我使用的是 NewtonSoft 库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2018-12-18
    相关资源
    最近更新 更多