JSON.NET serializeObject vb.net错误

时间:2014-12-07 13:57:10

标签: json vb.net class serialization json.net

Public Class Json_Info
    Public fruit As Json_Info_Fruit
End Class

Public Class Json_Info_Fruit
    Public aa As String
    Public ab As Integer
End Class

Public Class Main
    Private Sub Example()
        Dim fruitInfo As New Json_Info
        fruitInfo.fruit.aa = "apple" 'Error On This Line
        fruitInfo.fruit.ab = 1

        Dim output As String = JsonConvert.SerializeObject(loginInfo)
        MsgBox(output)
    End Sub
End Class

错误 fruitInfo.fruit.aa =“apple”

怎么了? (什么.. json.net上的所有例子都是C#例子。没有人vb.net。这么难学)

我需要做..

{
    "fruit": {
        "aa": "apple",
        "ab": 1
    }
}

我的英语不好:P帮帮我

1 个答案:

答案 0 :(得分:1)

您永远不会初始化fruitInfo.fruit,并且没有Json_Info构造函数来执行此操作,因此fruit属性最初为Nothing

或者:

  1. 添加构造函数以初始化它,或

  2. 如果您希望每次使用,请务必执行此操作:

    Dim fruitInfo As New Json_Info
    fruitInfo.fruit = New Json_Info_Fruit        ' This is the new line
    fruitInfo.fruit.aa = "apple"
    
  3. 可能您可以在New成员的声明中使用fruit关键字,我现在不用VB.Net和MSDN不是很有用:

    Public fruit As New Json_Info_Fruit
    

    但是,请再次仔细检查。