0

I have a JSON string. which has a part like this

    "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.]"
          }
        ]
      }
    }

However, sometimes that part is an array as well. like

    "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."
            }
          ]
        }
      }

          ]

Is there a generic way that I can Deserlize both JSON without defining different objects? So like, I define an array of Result object, and when there is single entity, it will create one index in the object array, and when there are multiple, it may create multiple indexes in the array.

Ultimate goal is to use object object and use that to parse both single Result object or multiple.

Is that something possible? thanks Sameers

Sameers Javed
  • 342
  • 2
  • 5
  • 16
  • 1
    Which serializer are you using? It is definitely possible with [Json.Net](http://james.newtonking.com/json), using a `JsonConverter`. See [this question](http://stackoverflow.com/q/18994685/10263) for a solution in C#. You should be able to translate it to vb.net. – Brian Rogers Nov 17 '14 at 00:56

1 Answers1

0

Thanks Brian Rogers

The answer was in your link.

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

I converted that to VB.NET using this code.

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

and applied this attribute to my data definition.

    <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

and its all good now. thanks Sameers

Yes, I was using NewtonSoft library

Community
  • 1
  • 1
Sameers Javed
  • 342
  • 2
  • 5
  • 16