3

I am a c# developer and have the requirement to work in vb.net project. I am facing a simple issue I need to convert a class object to json string in vb.net.Problem is when I check the string after conversion I am getting output as:

[{},{},{}]

I am trying to store value of 3 objects into it but I am getting 3 empty objects {}. My code is like this:

Imports System.Web.Script.Serialization

Partial Class test
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim serializer As New JavaScriptSerializer
        Dim msgObj As msg
        Dim loginList As New List(Of msg)()

        msgObj = New msg("mubashir", True)
        loginList.Add(msgObj)
        msgObj = New msg("yasir", False)
        loginList.Add(msgObj)
        msgObj = New msg("umar", True)
        loginList.Add(msgObj)
        Dim s As String = serializer.Serialize(loginList)
        Response.Write(s)

    End Sub
End Class
Public Class msg
    Dim message As String
    Dim status As Boolean
    Sub New(ByRef Messag As String, ByVal Stat As Boolean)



        Me.message = Messag
        Me.status = Stat

    End Sub
End Class

2 Answers 2

5

message, status need to declare as Property.

Public Class msg
      Public Property message() As String
      Public Property status() As Boolean
      Sub New(ByRef Messag As String, ByVal Stat As Boolean)
            Me.message = Messag
            Me.status = Stat    
      End Sub
End Class
Sign up to request clarification or add additional context in comments.

2 Comments

Its working, thx Nguyen Kien...but may I know reason why it wasnot working. I think private class members are not serialized.
Of course, private member is not serialize, even in c#
2

It looks like it's your msg class at fault here, as you have declared two fields rather than two properties:

Public Class msg
    Public Property message() As String
    Public Property status() As Boolean
    Sub New(ByRef Messag As String, ByVal Stat As Boolean)
        Me.message = Messag
        Me.status = Stat

    End Sub
End Class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.