将 vb.net class 对象转换为 vb.net 中的 JSON 字符串

Convert vb.net class object to JSON string in vb.net

我是一名 C# 开发人员,需要在 vb.net 项目中工作。我面临一个简单的问题,我需要将 class 对象转换为 vb.net 中的 json 字符串。问题是当我在转换后检查字符串时,我得到的输出为:

[{},{},{}]

我试图将 3 个对象的值存储到其中,但我得到的是 3 个空对象 {}。 我的代码是这样的:

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

message,status需要声明为属性.

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

看来是您的 msg class 出了问题,因为您声明了两个字段而不是两个属性:

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