Link to home
Start Free TrialLog in
Avatar of JonyTolengo
JonyTolengoFlag for Argentina

asked on

I have a problem when deserialize a JSON string in my VB.NET client

Hi,

I have this JSON string, generated by my web services.

{
   "status":"OK",
   "userId":"CSDEMO",
   "name":"Administrador Del Sistema",
   "rol":0,
   "Areas":[
      {
         "Id":1,
         "Name":"COSTOS_Y_GASTOS"
      },
      {
         "Id":2,
         "Name":"VENTAS_BULK"
      },
      {
         "Id":3,
         "Name":"VENTAS_CR"
      },
      {
         "Id":4,
         "Name":"CARTERA_ANEJAMIENTOS"
      },
      {
         "Id":5,
         "Name":"BULK"
      },
      {
         "Id":6,
         "Name":"VENTAS_GASES_ENVASADOS"
      },
      {
         "Id":7,
         "Name":"COSTOS_Y_GASTOS_CR"
      },
      {
         "Id":8,
         "Name":"CILINDROS"
      },
      {
         "Id":9,
         "Name":"VENTAS_INTEGRADAS_GENVASADOS"
      },
      {
         "Id":10,
         "Name":"COSTOS_Y_GASTOS_JDE_90"
      }
   ]
}

Following the code to deserialize the JSON text.

-----------------------------------------------------------------------------------------------------------------------  '  

Dim TextoJason As String = HERE THE JSON STRING  


Dim TheMobileUser As New MobileUser

Dim ms As New MemoryStream(System.Text.Encoding.Unicode.GetBytes(TextoJason))
Dim serializer As System.Runtime.Serialization.Json.DataContractJsonSerializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(TheMobileUser.GetType())

TheMobileUser = serializer.ReadObject(ms)
ms.Close()

-----------------------------------------------------------------------------------------------------------------------
 
This is the class I used in the client side, (exactly as I definied in the server side to serialize the to JSON string).

Public Class MobileUser

    Private _status As String

    Public Property status() As String

        Get

            Return _status

        End Get

        Set(ByVal value As String)

            _status = value

        End Set

    End Property

    Private _userId As String

    Public Property userId() As String

        Get

            Return _userId

        End Get

        Set(ByVal value As String)

            _userId = value

        End Set

    End Property

    Private _name As String

    Public Property name() As String

        Get

            Return _name

        End Get

        Set(ByVal value As String)

            _name = value

        End Set

    End Property

    Private _rol As Integer

    Public Property rol() As Integer

        Get

            Return _rol

        End Get

        Set(ByVal value As Integer)

            _rol = value

        End Set

    End Property

    Private _Areas As List(Of Area)  

    Public Property Areas As List(Of Area)

        Get

            Return _Areas

        End Get

        Set(ByVal value As List(Of Area))

            _Areas = value

        End Set

    End Property

    Public Sub New()
        Areas = New List(Of Area)
    End Sub

End Class


Public Class Area

    Private _Id As Integer
    Private _Name As String

    Public Property Id() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = Name
        End Set
    End Property

    Public Sub New()
    End Sub

    Public Sub New(ByVal Id As Integer, ByVal Name As String)
        _Name = Name
        _Id = Id
    End Sub

End Class


The problem is becouse the NAME of the Areas is not loaded into the class (become NOTHING)

Please, see the attached picture:

Thanks in advance,

Jonny
 User generated image
Avatar of leakim971
leakim971
Flag of Guadeloupe image

there's a mistake with the previous class, replace :
    ' This Clase is Ok here?

    Public Class Area

        Private _id As Integer
        Private _title As String

        <DataMember(Name:="id")> _
        Public Property id() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property

        <DataMember(Name:="title")> _
        Public Property total() As Integer
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        Public Sub New()
        End Sub

        Public Sub New(ByVal id As Integer, ByVal title As String)
            Me._title = title
            Me._id = id
        End Sub


    End Class

Open in new window


by :

    ' This Clase is Ok here?

    <DataContract()> _
    Public Class Area

        Private _id As Integer
        Private _title As String

        <DataMember(Name:="id")> _
        Public Property id() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property

        <DataMember(Name:="title")> _
        Public Property title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        Public Sub New()
        End Sub

        Public Sub New(ByVal id As Integer, ByVal title As String)
            Me._title = title
            Me._id = id
        End Sub


    End Class

Open in new window


I apologize
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JonyTolengo

ASKER

Thansk so much...  its works fine...
No hay de queso, no mas de papa