vb.net返回具有多种类型的json对象? [英] vb.net return json object with multiple types?

查看:316
本文介绍了vb.net返回具有多种类型的json对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"
data.rows(1).id = 2
data.rows(1).name = "steve"
data.rows(2).id = 3
data.rows(2).name = "fred"

我不知道该怎么做.我已经放弃了简单的类型和简单的数组,但是从来没有像这样的对象.

I have no idea how to do this. I've returend simple types and simple arrays, but never an object like this.

数据源是一个sql数据库.目标是javascript/ajax函数.目前,我已经成功地将行本身作为数据集返回了并且可以正常工作,但是我需要添加计数和其他两个父级"变量.

The data source is a sql Database. The target is a javascript/ajax function. I'm currently successfully returning the rows themselves as a dataset and it works, but I need to add the count and a couple other "parent level" variables.

为了全面披露,下面的代码可以正常工作:

For the sake of full disclosure, here is the code that is working:

<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

And I'm trying to consume it with FlexiGrid. I've been working at it for a few hours with no luck. I basically need to convert the PHP at the following site to .net

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

推荐答案

我认为,仅创建几个类并将数据从数据库移到这些类中,您会好得多.例如:

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

然后将方法的返回类型更改为MyDataClass并将返回值更改为:

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

        Return New MyDataClass(1, ds)

这篇关于vb.net返回具有多种类型的json对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆