有多种类型vb.net返回JSON对象?(vb.net return json object wit

2019-08-05 23:32发布

我需要从一个看起来像这样的网络服务返回一些数据:

data.page = 1
data.count = 12883
data.rows(0).id = 1
data.rows(0).name = "bob"
data.rows(1).id = 2
data.rows(1).name = "steve"
data.rows(2).id = 3
data.rows(2).name = "fred"

我不知道如何做到这一点。 我returend简单类型和简单的数组,但从来没有像这样的对象。

数据源是一个SQL数据库。 目标是一个JavaScript / AJAX功能。 我目前正在顺利返回行自己作为一个数据集和它的作品,但我需要添加数量和其他几个“父母级”的变量。

为了充分披露的缘故,这里是工作的代码:

<WebMethod()> _
Public Function rptPendingServerRequests() As DataSet
    Dim connetionString As String
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim adapter As New SqlDataAdapter
    Dim ds As New DataSet
    Dim sql As String

    connetionString = "..."
    sql = "SELECT usm_request.request_id, usm_request.status, usm_request.req_by_user_id " +
        "FROM usm_request " +
        "WHERE usm_request.request_id in " +
        "(SELECT distinct(usm_request.request_id) from usm_request, usm_subscription_detail WHERE usm_request.request_id = usm_subscription_detail.request_id " +
        "AND usm_subscription_detail.offering_id = 10307) ORDER BY usm_request.request_id DESC"
    connection = New SqlConnection(connetionString)

    Try
        connection.Open()
        command = New SqlCommand(sql, connection)
        adapter.SelectCommand = command
        adapter.Fill(ds)
        adapter.Dispose()
        command.Dispose()
        connection.Close()

        Return ds

    Catch ex As Exception
    End Try
End Function

我试图用FlexiGrid来使用它。 我在这工作了,没有运气了几个小时。 基本上,我需要的PHP转换在以下站点.NET

http://code.google.com/p/flexigrid/wiki/TutorialPropertiesAndDocumentation

Answer 1:

我认为你将是关闭刚创建几个类,并从数据库将数据移动到这些类好得多。 例如:

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

然后改变方法MyDataClass的返回类型和改变返回到:

        Return New MyDataClass(1, ds)


文章来源: vb.net return json object with multiple types?