Json 序列化集合时的无限循环 (VB ASP.NET)

发布于 2024-09-16 18:33:28 字数 520 浏览 7 评论 0原文

我正在尝试使用 Web 服务从数据库表中返回用户集合的 Json。我只是 .NET 的新手(不到 1 周的经验),并且我不想使用 AJAX 的 UpdatePanel。我尝试过使用 JavaScriptSerializer 以及 Json.NET 进行序列化。这两种情况似乎都会产生无限循环。

我做错了什么?有更好的方法吗?我很感激任何建议。谢谢。

    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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-09-23 18:33:28

感谢 RPM1984 建议 DataContractJsonSerializer。这是工作代码:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文