开发者

JSON.Net Always returns {}

开发者 https://www.devze.com 2023-03-07 00:30 出处:网络
Foo Public Class Foo Private fooz As Object Private alao As ArrayList Public Sub New(ByVal pfooz) Me.fooz = New Object

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.开发者_如何学JAVAalao.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消