【问题标题】:How can a return JSON data for JQUERY datatable?如何为 JQUERY 数据表返回 JSON 数据?
【发布时间】:2019-05-01 04:44:09
【问题描述】:

如何生成 JSON 数据用于绑定 JQUERY 数据表?

我在 ASP.net Web 服务 (.asmx) 中使用以下代码

<WebMethod()> _
Public Function Getcdata() As String
    Dim dt As New DataTable()
    Using con As New SqlConnection(IDvar.Constr)
        Using cmd As New SqlCommand("Select * from COMPLAINTTYPE", con)

            con.Open()
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(dt)
            Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
            Dim rows As New List(Of Dictionary(Of String, Object))()
            Dim row As Dictionary(Of String, Object)
            For Each dr As DataRow In dt.Rows
                row = New Dictionary(Of String, Object)()
                rows.Add(row)
            Next
            Context.Response.Write(serializer.Serialize(dt))

            con.Close()
            cmd.Dispose()
            dt.Clear()
        End Using
    End Using
End Function

但这会返回错误。请检查我哪里错了

我在调用 web 方法时遇到的错误如下:

System.InvalidOperationException: A circular reference was detected while serializing an object of type &#39;System.Reflection.RuntimeModule&#39;.
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)

【问题讨论】:

    标签: jquery asp.net json vb.net datatable


    【解决方案1】:

    问题出现在这一行:

    Context.Response.Write(serializer.Serialize(dt))
    

    您正在尝试将DataTable 实例直接序列化为JavaScriptSerializer 实例,该实例接受IEnumerableDictionary(Of TKey, TValue) 等集合对象。通常你需要的是使用AsEnumerable()[Select]() LINQ方法转换成IEnumerable

    Dim list = dt.AsEnumerable().[Select](Function(row) New With { Key
        .id = row("COMPLAINTID"), Key
        .name = row("COMPLAINTNAME"), 
    
        ' other DataTable columns here
    }).ToList()
    
    Context.Response.Write(serializer.Serialize(list))
    

    但是由于您有一个包含要序列化的对象的Dictionary(Of String, Object) 列表,因此只需将字典传递给序列化程序,而不是传递DataTable

    Dim rows As New List(Of Dictionary(Of String, Object))()
    Dim row As Dictionary(Of String, Object)
    For Each dr As DataRow In dt.Rows
        row = New Dictionary(Of String, Object)()
        rows.Add(row)
    Next
    
    ' pass the list of dictionary here
    Context.Response.Write(serializer.Serialize(rows))
    

    旁注:

    您可以尝试 JSON.NET 中的JsonConvert.SerializeObject() 方法来序列化包含Dictionary(Of TKey, TValue) 对象的列表,它比JavaScriptSerializer.Serialize() 方法具有更好的字典处理能力。请注意,您应该检查输出以确保 JSON 字符串的格式正确。

    【讨论】:

    • 非常感谢您的回复。正如您所建议的,我尝试序列化行,但它返回一个空文档。我会尝试使用 JsonConvert 再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2020-09-05
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 2016-04-16
    • 2023-03-15
    相关资源
    最近更新 更多