JSON.Net 始终返回 {}

发布于 2024-11-08 00:07:47 字数 1210 浏览 0 评论 0原文

Foo

Public Class Foo
    Private fooz As Object
    Private alao As ArrayList

    Public Sub New(ByVal pfooz)
        Me.fooz = New Object
        Me.alao = New ArrayList()

        Me.alao.Add(1)
        Me.alao.Add(2)
        Me.alao.Add(3)
        Me.alao.Add(4)
        Me.alao.Add(5)
    End Sub
End Class

Bar

Public Class Bar
    Private baz As Integer
    Private bar As String
    Private foo As Foo

    Public Sub New(ByVal pbaz, ByVal pbar, ByVal pfoo)
        Me.baz = pbaz
        Me.bar = pbar
        Me.foo = pfoo
    End Sub
End Class

My Form

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim foo As Foo = New Foo(New Object)
        Dim bar As Bar = New Bar(1, "FOOOOOOOOOOOOO, bar?", foo)
        Dim fooString As String
        Dim barString As String

        fooString = Newtonsoft.Json.JsonConvert.SerializeObject(foo)
        barString = Newtonsoft.Json.JsonConvert.SerializeObject(bar)

        MsgBox(fooString)
        MsgBox(barString)
    End Sub
End Class

我总是得到 {} 作为答案。怎么了?

Foo

Public Class Foo
    Private fooz As Object
    Private alao As ArrayList

    Public Sub New(ByVal pfooz)
        Me.fooz = New Object
        Me.alao = New ArrayList()

        Me.alao.Add(1)
        Me.alao.Add(2)
        Me.alao.Add(3)
        Me.alao.Add(4)
        Me.alao.Add(5)
    End Sub
End Class

Bar

Public Class Bar
    Private baz As Integer
    Private bar As String
    Private foo As Foo

    Public Sub New(ByVal pbaz, ByVal pbar, ByVal pfoo)
        Me.baz = pbaz
        Me.bar = pbar
        Me.foo = pfoo
    End Sub
End Class

My Form

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim foo As Foo = New Foo(New Object)
        Dim bar As Bar = New Bar(1, "FOOOOOOOOOOOOO, bar?", foo)
        Dim fooString As String
        Dim barString As String

        fooString = Newtonsoft.Json.JsonConvert.SerializeObject(foo)
        barString = Newtonsoft.Json.JsonConvert.SerializeObject(bar)

        MsgBox(fooString)
        MsgBox(barString)
    End Sub
End Class

I always get {} as answer. What's wrong?

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

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

发布评论

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

评论(1

帅的被狗咬 2024-11-15 00:07:47

这些类都没有任何 Public 属性或字段。如果您希望将字段表示为应出现在 JSON 字符串中的值,请将它们公开。否则,就序列化程序而言,您将为其提供没有有趣值的对象。

Neither of these classes have any Public properties or fields. If you want to represent your fields as values that should be present in a JSON string, make them public. Otherwise, as far as the serializer is concerned, you're giving it objects with no interesting values on them.

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