none
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

    Hans

    -------------------
    JSON string

    {"realtime_accesses":{"@type":"array","realtime_access":[{"level":{"@type":"integer","#text":"1"},"marketID":"6"},{"level":{"@type":"integer","#text":"1"},"marketID":"11"}]}}

    Same as XML

      <realtime-accesses type="array">
        <realtime-access>
             <level type="integer">1</level>
            <marketID>6</marketID>
         </realtime-access>
        <realtime-access>
             <level type="integer">1</level>
            <marketID>11</marketID>
         </realtime-access>
         </realtime-accesses>


    ------------------------------------------------------------------------------------
    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)

            ms.Close()
            Return reply
      End Function

    Tuesday, April 3, 2012 1:35 PM

Answers

All replies