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
 NAME = NOTHING
JonyTolengoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
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
0
leakim971PluritechnicianCommented:
Full code :

Imports System.Data.Common
Imports System.Runtime.Serialization
Imports System.Data.SqlClient
Imports System.IO
Imports System.Runtime.Serialization.Json
Imports System.Text

Module Module1

    Sub Main()
        ManageMobileUsers("23847", "leakim", "active", 1)
    End Sub


    Public Function ManageMobileUsers(UserId As String, UserName As String, UserStatus As String, UserRol As Integer) As String

        Dim TheMobileUser As New MobileUser

        TheMobileUser.userId = UserId       ' USER ID
        TheMobileUser.name = UserName       '  USER NAME
        TheMobileUser.status = UserStatus   '  USER STATUS
        TheMobileUser.rol = UserRol         ' USER ROL

        'Now I will load de areas for the user  (0 to N records).

        Dim myConn As SqlConnection = New SqlConnection("Data Source=FACEBOOK;Initial Catalog=SteeveJobs;User ID=sa;password=imaveryveryveryverystrongpassword;")
        Dim myCmd As SqlCommand = myConn.CreateCommand()
        myCmd.CommandText = "Select AreaId, AreaName From Areas_Usr Where UserId = '" & UserId & "'"
        myConn.Open()
        Dim myReader As SqlDataReader = myCmd.ExecuteReader()

        Do While myReader.Read()
            TheMobileUser.Areas.Add(New Area(myReader.GetInt32(0), myReader.GetString(1)))
        Loop

        Dim ms As MemoryStream = New MemoryStream()
        Dim ser As DataContractJsonSerializer = New DataContractJsonSerializer(GetType(MobileUser))
        ser.WriteObject(ms, TheMobileUser)
        Dim json() As Byte = ms.ToArray()
        ms.Close()
        Dim TheStringJson As String = Encoding.UTF8.GetString(json, 0, json.Length)

        Return TheStringJson

    End Function



    '  My Clase

    <Runtime.Serialization.DataContract()> _
    Public Class MobileUser

        Private _status As String

        <Runtime.Serialization.DataMember(name:="status")> _
        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

        <Runtime.Serialization.DataMember(name:="userId")> _
        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

        <Runtime.Serialization.DataMember(name:="name")> _
        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

        <Runtime.Serialization.DataMember(name:="rol")> _
        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)

        <Runtime.Serialization.DataMember(name:="options")> _
        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

    ' 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 Me._id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property

        <DataMember(Name:="title")> _
        Public Property title() As String
            Get
                Return Me._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


End Module

Open in new window

Clipboard03.jpg
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JonyTolengoAuthor Commented:
Thansk so much...  its works fine...
0
leakim971PluritechnicianCommented:
No hay de queso, no mas de papa
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.