Json序列化器vs .net序列化类的JQuery AJAX结果数据

本文关键字:序列化 JQuery AJAX 数据 结果 vs net Json | 更新日期: 2023-09-27 18:16:43

使用JQuery AJAX将数据从。net (c#, vb.net) Web Service传递到客户端最有效的方法是什么?

A)使用Newtonsoft JSON序列化例如
 <WebInvoke(Method:="*", ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/abc", BodyStyle:=WebMessageBodyStyle.Bare)>
    Public Function GetDontMissItems(JsonParams As RequestDataTypes.DontMissParams) As String
  objDontMissItems = Helper.Instance.GetDontMissNews(JsonParams.FeaturedCategoryId, QtyOfNumberOfItems, JsonParams.Randomize, If(JsonParams.NotInIDs = Nothing, "", JsonParams.NotInIDs))
   Dim strSerialzed As String = JsonConvert.SerializeObject(objDontMissItems)
   Return strSerialzed
   End Function

B)将一个序列化的类从web服务传递给AJAX调用,例如
<Serializable>
Public Class clsPoll
    Public Property answerID As Integer
    Public Property questionRef As String
    Public Property votePercentage As String
End Class

<WebMethod(EnableSession:=True)> _
    Public Function InsertPoll(ByVal jsonData As clsPoll) As List(Of clsPoll)
    Dim dtVotests As DataSet = objAnswer.CalculateVote(jsonData.questionRef, 1)
    Dim lstPoll As New List(Of clsPoll)
        For Each drVotests As DataRow In dtVotests.Tables(0).Rows
            Dim objPollTemp As New clsPoll
            objPollTemp.answerID = drVotests("id")
            objPollTemp.questionRef = jsonData.questionRef
            objPollTemp.votePercentage = drVotests("p")
            lstPoll.Add(objPollTemp)
        Next
    Return lstPoll
End Function

Json序列化器vs .net序列化类的JQuery AJAX结果数据

在服务器端像这样传递

return new JavaScriptSerializer().Serialize(new { errMsg = "test" });