We help IT Professionals succeed at work.

I want to generate the following JSON file.

Hello,
 
I want to generate the following JSON file.

{
    "status": "ok",
    "userId": 23847,
    "name": "Marco Casario",
    "rol": 0,
    "options": [{
        "id": 1,
        "title": "Ventas"
    }, {
        "id": 2,
        "title": "Compras"
    }, {
        "id": 3,
        "title": "Recursos Humanos"
    }]
}


I have problems to create this part:


    "options": [{
        "id": 1,
        "title": "Ventas"
    }, {
        "id": 2,
        "title": "Compras"
    }, {
        "id": 3,
        "title": "Recursos Humanos"
    }]


I don´t know what type exactly I must to use, and how to load it.


Following the function I am using to loads the data and serialize it to JSON

Also you can found the clase that I use.

Anybody could help me please?

Thanks in advance,

Tommy





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 MyData As New DataSet()

' Every AREA has and ID, NAME   (1=sales, 2=purchase, etc.)

Command.CommandText = "Select AreaId, AreaName From Areas_Usr Where UserId = '" & UserId & "'"
 
Adapter.SelectCommand = Command
 
Adapter.Fill(MyData)    ' MyData loaded with the ares for the user

For I As Integer = 0 To MyData.Tables(0).Rows.Count - 1

     ' here I don´t know how to load this tipe of data

      TheMobileUser.Areas.Add(??????????)

Next
 
' Here I will serialize to JSON

Dim Serializador As New System.Runtime.Serialization.Json.DataContractJsonSerializer(TheMobileUser.GetType)
 
Dim Ms As New System.IO.MemoryStream

Serializador.WriteObject(Ms, TheMobileUser)
 
Dim TheStringJson As String = System.Text.Encoding.UTF8.GetString(Ms.ToArray)

Return TheStringJson
 
End Function
 


'  My Clase
 
 < Runtime.Serialization.DataContract()> _
 Public Class MobileUser
 
Private _status As String
 
<Runtime.Serialization.DataMember(Order:=1)> _
 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(Order:=2)> _
 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(Order:=3)> _
 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(Order:=4)> _
 Public Property rol() As Integer
 
Get
 
Return _rol
 
End Get
 
Set(ByVal value As Integer)
 
_rol = value
 
End Set
 
End Property
 


 ' GET and SET is needed here?
 
Private _Areas As List(Of Area)
 
<Runtime.Serialization.DataMember(Order:=5)> _
 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() ' ----------------->> this is right here?
 
Areas = New List(Of Area)
 
End Sub
 
End Class
 
' This Clase is Ok here?
 
Public Class Area
 
         Public Property Id As Integer
         Public Property Name As String
 
End Class
 
Comment
Watch Question

Multitechnician
Distinguished Expert 2019
Commented:
something like :

        For I As Integer = 0 To MyData.Tables(0).Rows.Count - 1
            Dim area As New Area(MyData.Tables(0).Rows(I).Item(0), MyData.Tables(0).Rows(I).Item(1))
            TheMobileUser.Areas.Add(area)
        Next

Open in new window


with something like :
' 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 Integer)
                _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

Open in new window

Top Expert 2007

Commented:
Visit JSON Org for all things JSON.

Author

Commented:
Hi,

Thanks, works fine exactly as I want, but now I have a little problem to deserialize the Json text in a class. (in the client side)

This is the JSON text (generated by the function above):

{
   "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 received, into a class. (in the client side)

-----------------------------------------------------------------------------------------------------------------------
 
 '  This function get the JSON text from the server
Dim TextoJason As String = MyWS.ManageMobileUsers()  


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

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
leakim971Multitechnician
Distinguished Expert 2019

Commented:
If your main question is answered, please open a new one choosing a appropriate title, it will help somebody else searching an answer.
Top Expert 2007

Commented:
You're using VB on the client?

Do yourself a favor and use JavaScript. Why? Because JSON is JavaScript.

Author

Commented:
Thanks the answer was excellent.
Thanks so much..!!!
leakim971Multitechnician
Distinguished Expert 2019

Commented:
You're very welcome!
Hope to have time to answer your next question, thanks for the points, have a good time!
See you soon