vb.net return json object with multiple types?

后端 未结 1 1142
耶瑟儿~
耶瑟儿~ 2021-01-26 01:25

I need to return some data from a web service that looks something like this:

data.page = 1
data.count = 12883
data.rows(0).id = 1
data.rows(0).name = \"bob\"
da         


        
相关标签:
1条回答
  • 2021-01-26 01:40

    I think that you would be much better off just creating a couple of classes and moving the data from the database into these classes. For example:

    Public Class MyDataClass
        Public Property Page As Integer
    
        Public ReadOnly Property Count As Integer
            Get
                If Me.Rows IsNot Nothing Then
                    Return Me.Rows.Count
                Else
                    Return 0
                End If
            End Get
        End Property
    
        Public Property Rows As List(Of MyDataRow)
    
        ' Parameterless constructor to support serialization.
        Public Sub New()
            Me.Rows = New List(Of MyDataRow)
        End Sub
        Public Sub New(wPage As Integer, ds As DataSet)
            Me.New()
    
            Me.Page = wPage
    
            For Each oRow As DataRow In ds.Tables(0).Rows
                Dim oMyRow As New MyDataRow
    
                oMyRow.Id = oRow("id")
                oMyRow.Name = oRow("Name")
    
                Me.Rows.Add(oMyRow)
            Next
        End Sub
    End Class
    
    Public Class MyDataRow
        Public Property Id As Integer
        Public Property Name As String
    
        ' Parameterless constructor to support serialization
        Public Sub New()
    
        End Sub
    End Class
    

    Then change the return type of the method to MyDataClass and change the return to:

            Return New MyDataClass(1, ds)
    
    0 讨论(0)
提交回复
热议问题