0

SQL 呼び出しで .asmx ページから純粋な JSON を取得しようとしています。手に入らないようです。私が見つけたすべての投稿は、C#であるか、私が求めているものではありません。シリアル化により、1 次元配列の問題が発生します。小さなJavaアプリ用に、Webサーバー上のMSアクセスデータベースからデータを取得する必要があります。

<%@ 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 に答える