How deseralize JSON and XML with ARRAY type in VB.NET RRS feed

  • Question

  • Hi,

    I try to  deseralize JSON and XML with ARRAY type  in VB.NET.
    It dont work. It is fine when there are no ARRAY type in the strings, but else it fails.
    I also tried with datacontract- but no improvements.

    What am i doing wrong?

    best regards


    JSON string


    Same as XML

      <realtime-accesses type="array">
             <level type="integer">1</level>
             <level type="integer">1</level>

    Imports System.Xml.Serialization
    Imports System.Runtime.Serialization
    Imports System.Runtime.Serialization.Json
    Imports Newtonsoft.Json

    Public Class realtime_accesses
        Public Property realtime_access() As realtime_access()
    End Class

    Public Class realtime_access
        Public Property marketID() As Integer
        Public Property level() As Integer
    End Class

    private sub test(json as string,xml as string)

    Dim x As New realtime_accesses
         x = Newtonsoft.Json.JsonConvert.DeserializeObject(Of realtime_accesses)(json)   ' will not populate X
         x = xml_Deserialize(Of realtime_accesses)(xml)  ' creates an error
    end sub


     Public Function xml_Deserialize(Of T)(ByVal xml As String) As T
            Dim ms As New MemoryStream(Encoding.Unicode.GetBytes(xml))

            Dim reply As T = Activator.CreateInstance(Of T)()

            Dim serializer As New XmlSerializer(reply.[GetType]())

            reply = DirectCast(serializer.Deserialize(ms), T)

            Return reply
      End Function

    Tuesday, April 3, 2012 1:35 PM


All replies