I am trying to use a Web Service to return Json for开发者_运维问答 a Collection of Users from a Database Table. I'm only new to .NET (< 1 week experience), and I don't want to use the UpdatePanel for AJAX. I have tried using the JavaScriptSerializer as well as Json.NET to serialize. Both cases seem to spawn an Infinite Loop.
What am I doing wrong? Is there a better way to do this? I appreciate any suggestions. Thanks.
Dim myUser As New HagarDB.Users
myUser.Read()
'Dim jsSerializer As New System.Web.Script.Serialization.JavaScriptSerializer
'Dim sbUsers As New System.Text.StringBuilder
'jsSerializer.Serialize(myUser, sbUsers)
Dim json = JsonConvert.SerializeObject(myUser, Formatting.Indented)
Thanks to RPM1984 for suggesting DataContractJsonSerializer. Here is the working code:
Public Function GetUsers() As String
Dim myUser As New HagarDB.Users
Dim jsonSerializer As New DataContractJsonSerializer(GetType(HagarDB.Users))
Dim stream As New MemoryStream()
myUser.Read()
jsonSerializer.WriteObject(stream, myUser)
Dim json As String = Encoding.[Default].GetString(stream.ToArray())
stream.Close()
Return json
End Function
精彩评论