【问题标题】:Class to JSON VB.NetJSON VB.Net 的类
【发布时间】:2018-11-05 17:21:00
【问题描述】:

所以我有一个要序列化到 Json 的类。一切顺利,直到这个嵌套类,它给了我一个 System.NullReferenceException = {"Object reference not set to an instance of an object."}。编写代码时,智能感知识别嵌套类,但显然我在某处缺少声明。

根类:

    Public Class RootObject
    Private _metadata As List(Of Metadata)
    Public Property metadata() As List(Of Metadata)
    Get
        Return _metadata
    End Get
    Set(ByVal value As List(Of Metadata))
        _metadata = value
    End Set
    End Property

    Private _test_gl As List(Of TestGl)
    Public Property test_gl() As List(Of TestGl)
    Get
        Return _test_gl
    End Get
    Set(ByVal value As List(Of TestGl))
        _test_gl = value
    End Set
    End Property
End Class

这是 TestGl 类的定义:

    Public Class TestGl
    Private _ref_key_3 As String
    <JsonProperty("ref-key-3")> _
    Public Property ref_key_3() As String
    Get
        Return _ref_key_3
    End Get
    Set(ByVal value As String)
        _ref_key_3 = value
    End Set
    End Property

    Private _currency_amount As CurrencyAmount
    <JsonProperty("currency-amount")> _
    Public Property currency_amount() As CurrencyAmount
    Get
        Return _currency_amount
    End Get
    Set(ByVal value As CurrencyAmount)
        _currency_amount = value
    End Set
    End Property
End Class

最后是 CurrencyAmount 类:

    Public Class CurrencyAmount
    Private _currency As String
    <JsonProperty("currency")> _
    Public Property currency() As String
    Get
        Return _currency
    End Get
    Set(ByVal value As String)
        _currency = value
    End Set
    End Property

    Private _amount As String
    <JsonProperty("amount")> _
    Public Property amount() As String
    Get
        Return _amount
    End Get
    Set(ByVal value As String)
        _amount = value
    End Set
    End Property
End Class

下面是用数据填充对象的代码:

        Dim Root As RootObject
    Dim Meta_Data As New List(Of Metadata)()
    Dim Test_Gl As New List(Of TestGl)()

    Root = New RootObject
    Root.metadata = New List(Of Metadata)()
    Root.test_gl = New List(Of TestGl)
    Meta_Data = Root.metadata
    Test_Gl = Root.test_gl

在这里我给它赋值:

                        Test_Gl.Add(New AccountGl)
                    Test_Gl(ItemNo).ref_key_3 = "test"
                    Test_Gl(ItemNo).currency_amount.currency = "EUR"
                    Test_Gl(ItemNo).currency_amount.amount = "100"

分配currency_amount.currency 的行出错了,我已经看了好几个小时了。我没看到。 任何意见将不胜感激。

属性写得很完整,因为我需要在 VS2008 中处理这个项目。

【问题讨论】:

  • 这可能与 JSON 无关。没有序列化部分你能运行程序吗?
  • 如果 Test_Gl 是通过反序列化编码字符串创建的,请检查原始 JSON 是否包含货币金额的值。如果没有,您需要在引用它之前赋予它价值,就像@BlueMonkMN 建议的那样

标签: json vb.net class nested


【解决方案1】:

我怀疑您需要在某个地方将 _currency_amount 初始化为 CurrencyAmount 的新实例,我在任何地方都看不到 new CurrencyAmount。

我怀疑您并不想允许设置 currency_amount 属性,否则您应该在底部的示例分配代码中设置它。如果是这种情况,那么您可能甚至不应该为TestGl 定义Set 成员(它应该是ReadOnly,它只影响currency_amount 而不是_currency_amount.currency)。相反,您应该创建CurrencyAmount 的默认实例,并在TestGl 的构造期间将其分配给该字段。这可以很简单,只需将 _currency_amount 的声明更改为:

Private _currency_amount As New CurrencyAmount

或者,这可能是您需要与 JSON 可序列化对象一起使用的解决方案,您保留 Set 成员定义,并在测试代码中添加一行以在使用它之前初始化 currency_amount:

Test_Gl(ItemNo).currency_amount = new CurrencyAmount

【讨论】:

  • 谢谢,最后一行成功了。但是为什么我必须初始化 CurrencyAmount 类,当它是一个已经初始化的根类的嵌套类时?不应该已经在根目录初始化了吗?
  • @user9671207 每个引用类的声明都需要初始化为新的或已经存在的东西。嵌套声明与根级别声明没有什么不同。每一个都只代表一个指向另一个对象的指针/引用。嵌套类(也称为引用类型)的唯一内容是引用,而不是它所引用的实际实例/对象。所以根分配只分配空间来引用另一个对象,而不是实际存储该对象的空间。
猜你喜欢
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
  • 2015-11-07
  • 2011-11-04
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多