Выведенный JSON файл повторяет имя массива (VB. NET, Newtonsoft. JSON) - PullRequest
0 голосов
/ 09 января 2020

Я новичок в JSON и пытаюсь создать вывод JSON, идентичный этому, используя VB. NET и Newtonsoft. JSON

{
  "name": "trash",
  "icon": "trash_can",
  "display_name": "Taking out the trash",
  "display": [
    {
      "type": "text",
      "text": "Craft a Trash Can"
    },
    {
      "type": "recipe",
      "query": "trash_can"
    }
  ],
  "conditions": [
    {
      "type": "item",
      "item": "trash_can",
      "amount": 1
    }
  ],
  "rewards": [
    {
      "type": "item",
      "item": "cobblestone",
      "amount": 1
    }
  ]
}

Код, который я написал до сих пор выводит это:

  "name": "trash",
  "icon": "trash_can",
  "display_name": "Taking out the Trash",
  "display": {
    "display": [
      {
        "type": "item",
        "text": "Craft a Trash Can"
      }
    ]
  },
  "conditions": {
    "conditions": [
      {
        "type": "item",
        "item": "trash_can",
        "amount": 0
      }
    ]
  },
  "rewards": {
    "rewards": [
      {
        "type": "item",
        "item": "cobblestone",
        "amount": 1
      }
    ]
  }
}

Мой вывод повторяет термины «дисплей», «условия» и «награды».

Мой код выглядит следующим образом:

Imports Newtonsoft.Json

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim quest = New Quest()

        quest.Title = "trash"
        quest.Icon = "trash_can"
        quest.displayName = "Taking out the Trash"

        Dim varDisplay = New Display()
        varDisplay.Type = "item"
        varDisplay.Text = "Craft a Trash Can"
        quest.display.displayItems.Add(varDisplay)

        Dim varCondition = New Condition()
        varCondition.Type = "item"
        varCondition.Item = "trash_can"
        quest.conditions.conditionItems.Add(varCondition)

        Dim varReward = New Reward()
        varReward.Type = "item"
        varReward.Item = "cobblestone"
        varReward.Amount = 1
        quest.rewards.rewardItems.Add(varReward)

        Dim rawJSON = JsonConvert.SerializeObject(quest)

        Dim output As System.IO.StreamWriter
        output = My.Computer.FileSystem.OpenTextFileWriter(Application.StartupPath & "/trash.json", True)
        output.Write(rawJSON)
        output.Close()


    End Sub
End Class

Class Quest
    <JsonProperty(PropertyName:="name")>
    Public Property Title As String

    <JsonProperty(PropertyName:="icon")>
    Public Property Icon As String

    <JsonProperty(PropertyName:="display_name")>
    Public Property displayName As String

    <JsonProperty(PropertyName:="display")>
    Public Property display As DisplayItems = New DisplayItems

    <JsonProperty(PropertyName:="conditions")>
    Public Property conditions As ConditionItems = New ConditionItems

    <JsonProperty(PropertyName:="rewards")>
    Public Property rewards As RewardItems = New RewardItems
End Class

Public Class Display
    <JsonProperty(PropertyName:="type")>
    Public Property Type As String

    <JsonProperty(PropertyName:="text")>
    Public Property Text As String
End Class

Public Class DisplayItems
    <JsonProperty(PropertyName:="display")>
    Public Property displayItems As List(Of Display) = New List(Of Display)
End Class

Public Class Condition
    <JsonProperty(PropertyName:="type")>
    Public Property Type As String

    <JsonProperty(PropertyName:="item")>
    Public Property Item As String

    <JsonProperty(PropertyName:="amount")>
    Public Property Amount As Integer
End Class

Public Class ConditionItems
    <JsonProperty(PropertyName:="conditions")>
    Public Property conditionItems As List(Of Condition) = New List(Of Condition)
End Class

Public Class Reward
    <JsonProperty(PropertyName:="type")>
    Public Property Type As String

    <JsonProperty(PropertyName:="item")>
    Public Property Item As String

    <JsonProperty(PropertyName:="amount")>
    Public Property Amount As Integer
End Class

Public Class RewardItems
    <JsonProperty(PropertyName:="rewards")>
    Public Property rewardItems As List(Of Reward) = New List(Of Reward)
End Class

Буду признателен за любую помощь или совет по удалению этих элементов и оставлению только массивов!

1 Ответ

1 голос
/ 09 января 2020

Проблема в вашем коде заключается в том, как вы заполняете эти коллекции отображения, наград и условий. Согласно вашему коду, это ваша структура (то же самое объяснение относится к вознаграждениям и условиям):

Quest
|
|
|--DisplayItems (with json proerty set to display)
    |
    |
    |--- collection of Display (again, used the same json proerty name display)

Вам нужно это:

Quest
|
|
|--- collection of Display (use the json proerty name display here as its camel casing)

Вы можете попробовать удалить класс DisplayItems непосредственно имея

<JsonProperty(PropertyName:="display")>
Public Property displayItems As List(Of Display) = New List(Of Display) 

под квестом. То же самое относится и к другим (условиям и вознаграждениям).

Итак, пожалуйста, измените свой класс квестов, как показано ниже:

Class Quest
       ....
<JsonProperty(PropertyName:="display")>
Public Property displayItems As List(Of Display) = New List(Of Display)
       .....
End Class

, а затем измените свой щелчок кнопки, как показано ниже:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         ......

        Dim varDisplay = New Display With {.Type = "item", .Text = "Craft a Trash Can"}
        quest.displayItems.Add(varDisplay)

        ..........    
    End Sub
End Class
...