Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输出的JSON文件重复数组名(VB.NET,Newtonsoft.JSON)_.net_Json_Vb.net_Json.net - Fatal编程技术网

输出的JSON文件重复数组名(VB.NET,Newtonsoft.JSON)

输出的JSON文件重复数组名(VB.NET,Newtonsoft.JSON),.net,json,vb.net,json.net,.net,Json,Vb.net,Json.net,我是JSON新手,尝试使用VB.NET和Newtonsoft.JSON创建与此相同的JSON输出 { "name": "trash", "icon": "trash_can", "display_name": "Taking out the trash", "display": [ { "type": "text", "text": "Craft a Trash Can" }, { "type": "recipe",

我是JSON新手,尝试使用VB.NET和Newtonsoft.JSON创建与此相同的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
导入Newtonsoft.Json
公开课表格1
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
暗任务=新任务()
quest.Title=“垃圾”
quest.Icon=“垃圾桶”
quest.displayName=“取出垃圾”
Dim varDisplay=新显示器()
varDisplay.Type=“项”
varDisplay.Text=“制作垃圾桶”
quest.display.displayItems.Add(varDisplay)
Dim varCondition=新条件()
varCondition.Type=“项”
varCondition.Item=“垃圾桶”
quest.conditions.conditionItems.Add(varCondition)
Dim varReward=新奖励()
varReward.Type=“项目”
varReward.Item=“鹅卵石”
varReward.Amount=1
任务。奖励。奖励。添加(奖励)
Dim rawJSON=JsonConvert.SerializeObject(任务)
将输出变暗为System.IO.StreamWriter
output=My.Computer.FileSystem.OpenTextFileWriter(Application.StartupPath&“/trash.json”,True)
output.Write(rawJSON)
output.Close()
端接头
末级
职业探索
公共财产所有权作为字符串
作为字符串的公共属性图标
公共属性displayName为字符串
公共属性显示为DisplayItems=新的DisplayItems
作为条件项的公共属性条件=新条件项
作为奖励的公共财产奖励=新奖励
末级
公开课展示
公共属性类型为字符串
公共属性文本作为字符串
末级
公共类显示项
公共属性displayItems作为列表(显示项)=新列表(显示项)
末级
公共阶级状况
公共属性类型为字符串
作为字符串的公共属性项
公共财产金额为整数
末级
公共类条件项
公共属性条件项作为列表(条件)=新列表(条件)
末级
公共级别奖励
公共属性类型为字符串
作为字符串的公共属性项
公共财产金额为整数
末级
公开课奖励
公共财产作为(奖励)列表的回报=新的(奖励)列表
末级

任何关于移除这些物品并只留下阵列的帮助或建议都将不胜感激

代码中的问题是如何填充这些显示、奖励和条件集合。根据您的代码,这是您拥有的结构(奖励和条件也有相同的解释):

您需要的是:

Quest
|
|
|--- collection of Display (use the json proerty name display here as its camel casing)
您可以通过直接使用

<JsonProperty(PropertyName:="display")>
Public Property displayItems As List(Of Display) = New List(Of Display) 
<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