如何在 Visual Basic 中将 json 对象包装在顶级对象中

How to wrap a json object inside of a top level object in visual basic

我正在尝试将多个对象嵌套在一个主对象中,但我真的不确定该怎么做,尤其是在向该对象内部的对象中添加新对象时。

这是我目前所拥有的,当用户单击按钮并运行使用用户输入的数据然后将其提交到 json 文件中的过程时获取的子。

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim monsterName As String
        monsterName = TextBox2.Text
        TextBox2.Text = ""

        Dim monsterCB As Integer
        monsterCB = TextBox3.Text
        TextBox3.Text = ""

        Dim monsterType As String
        monsterType = ComboBox3.SelectedItem
        ComboBox3.Text = ""


        Dim Monster As New Monster

        Monster.MonsterName = monsterName
        Monster.MonsterCombatRating = monsterCB
        Monster.MonsterType = monsterType

        Dim output As String

        output = JsonConvert.SerializeObject(Monster)

        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter("C:\Program Files (x86)\D&DLoot\data.json", True)
        file.WriteLine(output)
        file.Close()



    End Sub

我还有一个 class.vb,它只包含已定义的属性,我试图将它们用作 class 中的数组,但我只是得到下面的输出,尽管我确信那是你应该如何让它们嵌套在数组中:

Friend Class Monster

    Dim Monsterdata = New String() {MonsterName, MonsterCombatRating, MonsterType}

    Public Property MonsterName As String
    Public Property MonsterCombatRating As Integer
    Public Property MonsterType As String

End Class

这会在我的 json 文件中输出一个新行:

{"MonsterName":"testing","MonsterCombatRating":2,"MonsterType":"Bad"}
{"MonsterName":"here is another test","MonsterCombatRating":12,"MonsterType":"Bad"}

每次提交都会像这样添加一个新行,但是 json 需要 1 个顶级评论,所以我们的想法是得到这样的结果作为最终结果,减去战利品数组作为另一个事件:

    {"Monsterlootdata":[
      { "Monstername":"testmonster1", "Loot":[{"lootname":"test1", "dropchance":"20", "rarity":"common"}, {"lootname":"test2", "dropchace":"5", "rarity":"rare"}]},
      { "Monstername":"testmonster2","Loot":[{"lootname":"test1", "dropchance":"40", "rarity":"common"}, {"lootname":"test2", "dropchance":"10", "rarity":"uncommon"},{"lootname":"test3", "dropchance":"5", "rarity":"rare"}]},
      { "Monstername":"testmonster3", "loot":[{"lootname":"test1", "dropchance":"80", "rarity":"common"}]}
]}

您想要的是一个具有 属性 集合的对象,这样当您序列化该对象时,它会创建您想要的 {"property": []} 输出。

举个例子,你想要的最终结果。它可以表示为以下 Visual Basic .NET class:

Public Class MonsterLoot
    Public Property lootname As String
    Public Property dropchance As String ' this should probably be an integer or double, but the JSON is representing it as a string so I didn't make any changes
    Public Property rarity As String
End Class

Public Class Monster
    Public Property Monstername As String
    Public Property Loot As List(Of MonsterLoot)
End Class

Public Class RootObject
    Public Property Monsterlootdata As List(Of Monster)
End Class

您可以通过执行以下操作创建对象的新实例:

Dim root = New RootObject() With {
    .Monsterlootdata = New List(Of Monster)({
        New Monster() With {
            .Monstername = "testmonster1",
            .Loot = New List(Of MonsterLoot)({
                New MonsterLoot() With {
                    .lootname = "test1",
                    .dropchance = "20",
                    .rarity = "common"
                }
            })
        }
    })
}

然后当你去序列化对象时,你会得到以下内容 JSON:

{
  "Monsterlootdata": [
    {
      "Monstername": "testmonster1",
      "Loot": [
        {
          "lootname": "test1",
          "dropchance": "20",
          "rarity": "common"
        }
      ]
    }
  ]
}

示例:https://dotnetfiddle.net/ILuel1