0

尝试使用 SQL 调用从 .asmx 页面获取纯 JSON。好像拿不到 我发现的每一篇文章要么是在 c# 中,要么不是我所追求的。序列化会导致一维数组问题。我需要从我的 Web 服务器上的 MS 访问数据库中获取小型 Java 应用程序的数据。

<%@ WebService class="GetDBStudent" %>

Imports System.Web
Imports System.Web.Services
Imports System.Xml
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports System.Data
imports System.Web.Script.Serialization

'<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="com.mcfrsit.GetDBStudent")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class GetDBStudent
Inherits System.Web.Services.WebService
Public Class Students
    Public StudentID As String
    Public LastName As String
    Public FirstName As String
    Public Affiliation As String
    Public ClassName As String
    Public DateCompleted As DateTime
End Class

<WebMethod()> _
<ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json)> _
Public Function GetArray() As Students()

    ' Create a connection string
    Dim DBConnection As String =        ConfigurationManager.ConnectionStrings("OdbcServices").ToString
    Dim sql As String = "SELECT * FROM ClassRecordsEnrollmentsQry WHERE [Affiliation] = 'DFRS'"
    Dim Conn As New ADODB.Connection()
    Dim rs As New ADODB.Recordset()

    Dim daTitles As New Data.OleDb.OleDbDataAdapter()

    Dim dsTitles As New DataSet("CrossTab")
    Conn.Open(DBConnection, "", "", -1)

    Try
        rs.Open(sql, DBConnection, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockBatchOptimistic, 1)

        daTitles.Fill(dsTitles, rs, "Students")
        'create object array, using row count as upper dim
        Dim objStudents As Students() = New Students(dsTitles.Tables(0).Rows.Count - 1) {}

        'loop through dataset to add data to object array
        Dim intRsCount As Int16
        For intRsCount = 0 To dsTitles.Tables(0).Rows.Count - 1
            Dim intCsCount As Int16
            For intCsCount = 0 To dsTitles.Tables(0).Columns.Count - 1
                'Dim strColName As String = dsTitles.Tables(0).Columns(intCsCount)
            Next
            objStudents(intRsCount) = New Students
            objStudents(intRsCount).StudentID = dsTitles.Tables(0).Rows(intRsCount)(0)
            objStudents(intRsCount).LastName = dsTitles.Tables(0).Rows(intRsCount)(1)
            objStudents(intRsCount).FirstName = dsTitles.Tables(0).Rows(intRsCount)(2)
            objStudents(intRsCount).Affiliation = dsTitles.Tables(0).Rows(intRsCount)(3)
            objStudents(intRsCount).ClassName = dsTitles.Tables(0).Rows(intRsCount)(4)
            objStudents(intRsCount).DateCompleted = dsTitles.Tables(0).Rows(intRsCount)(5)
        Next

        'Return new JavaScriptSerializer().Serialize(objStudents)
        Return objStudents

    Catch ex As Exception
        'Response.Write("Sorting is not supported in the view")
    Finally
        Conn.Close()
    End Try
End Function


End Class
4

1 回答 1

0

已解决:将函数作为对象返回并且工作正常...

但是返回的 JSON 字符串如下所示:

[{"text":"text2"}]

为什么在开始和结束处有额外的“[”和“]”?

于 2012-11-10T10:02:34.370 回答