Vb.net JSON写一维数组
JSON如下所示:Vb.net JSON写一维数组,vb.net,json.net,Vb.net,Json.net,JSON如下所示: "Items": { "defaultActionClass": "Weaponclass", "ItemObjects": [ { "Id": "M16", "Name": "EM16", "Description": "Some Description", "Icon": "Icons/pictures/1.png", "AnimationSet": "Set1", "Type": "Weapon",
"Items": {
"defaultActionClass": "Weaponclass",
"ItemObjects": [
{
"Id": "M16",
"Name": "EM16",
"Description": "Some Description",
"Icon": "Icons/pictures/1.png",
"AnimationSet": "Set1",
"Type": "Weapon",
"EquipmentClass": "Hands",
"SlotsCount": 2,
"Weight": 1,
"AP": 1,
"BurstFire": 1,
"AutoFire": 0,
"EffectiveRange": 8.0,
"MuzzleVelocity": 2.5,
"BaseAccuracy": 100.0,
"Mobility": 6.0,
"Damage": 30,
"DamageFalloff": 30,
"AmmoClips": [
"AmmoClip556NATOx30"
]
},
我正在努力解决的问题是:
"AmmoClips": [
"AmmoClip556NATOx30"
]
我可以读写文件的其余部分,但这个部分我找不到解决方案
我通过以下途径阅读:
Public Property AmmoClips As String()
Ammunition = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AmmoClips").ToString)
这将返回:
[
"AmmoClip556NATOx30"
]
然后,我尝试使用下面的方法将其写回,我尝试了几十种不同的方法,但都没有成功
Imports System.IO
Imports System.Windows
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Text
Public Class Form1
Dim GamePath As String
Dim Gamefile As String
Dim strjson As String
Dim obj As Object
Dim NextCount As Integer
Dim Ammunition
Dim AmmoOutput
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
GamePath = "E:\SteamLibrary\SteamApps\common\Jagged Alliance Flashback\game_Data\StreamingAssets\Original\Data\"
Gamefile = "Items\Weapons.json"
strjson = File.ReadAllText(GamePath & Gamefile)
'RichTextBox1.Text = strjson
Dim root As RootObject = JsonConvert.DeserializeObject(Of RootObject)(strjson)
Dim ID As List(Of String) = root.Items.ItemObjects.Select(Of String)(Function(tp) tp.Id).ToList()
cmbWeaponID.DataSource = ID
NextCount = ID.Count + 1
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Dim sb As New StringBuilder
'sb = sb.AppendLine
'sb = sb.Append("""AmmoClip44Magnumx6""").AppendLine
'sb = sb.AppendLine
'Dim sbarray As New JRaw
'sbarray = sb
AmmoOutput = New String() {Ammunition, Ammunition}
' MsgBox(AmmoOutput)
Dim sJsonObj As RootObject = JsonConvert.DeserializeObject(Of RootObject)(File.ReadAllText(GamePath & Gamefile))
sJsonObj.Items.ItemObjects.Add(New ItemObject With {
.Id = NextCount,
.Name = txtName.Text,
.Description = txtDescription.Text,
.Icon = "Icons/Weapons/M16.png",
.AnimationSet = "Rifle",
.Type = "Weapon",
.EquipmentClass = "Hands",
.SlotsCount = txtSlotCount.Text,
.Weight = txtWeight.Text,
.AP = txtAPtoFire.Text,
.BurstFire = txtBurstFire.Text,
.AutoFire = txtAutoFire.Text,
.EffectiveRange = txtEffectiveRange.Text,
.MuzzleVelocity = txtMuzzleVelocity.Text,
.BaseAccuracy = txtBaseAccuracy.Text,
.Mobility = txtMobility.Text,
.Damage = txtDamage.Text,
.DamageFalloff = txtDamageFalloff.Text,
.AmmoClips = AmmoOutput
})
'Serialize to JSON string.
Dim settings As New JsonSerializerSettings
settings.NullValueHandling = NullValueHandling.Ignore
settings.Formatting = Formatting.Indented
Dim strAddJson As String = JsonConvert.SerializeObject(sJsonObj, settings)
' Write to file.
File.WriteAllText(GamePath & Gamefile, strAddJson)
End Sub
Private Sub cmbWeaponID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbWeaponID.SelectedIndexChanged
'cmbWeaponID.Items.Add(weaponjson.SelectToken("Items").SelectTokens("ItemObjects[0].Id"))
Dim o As JObject = JObject.Parse(strjson)
Dim Type As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Type"))
Dim Name As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Name"))
Dim Description As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Description"))
Dim Icon As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Icon"))
Dim AnimationSet As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AnimationSet"))
Dim SlotsCount As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].SlotsCount"))
Dim Weight As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Weight"))
Dim APCost As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AP"))
Dim BurstFire As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].BurstFire"))
Dim AutoFire As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AutoFire"))
Dim EffectiveRange As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].EffectiveRange"))
Dim MuzzleVelocity As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].MuzzleVelocity"))
Dim BaseAccuracy As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].BaseAccuracy"))
Dim Mobility As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Mobility"))
Dim Damage As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Damage"))
Dim DamageFalloff As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].DamageFalloff"))
If AnimationSet = "Rifle" Or AnimationSet = "Handgun" Then
Dim Ammunition As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AmmoClips[0]"))
'MsgBox(Ammunition)
txtName.Text = Name
txtDescription.Text = Description
'picIcon.Image.
lblAnimationSet.Text = AnimationSet
txtSlotCount.Text = SlotsCount
txtWeight.Text = Weight
txtAPtoFire.Text = APCost
If BurstFire = 1 Then
chkBurstFire.Checked = True
Else
chkBurstFire.Checked = False
End If
If AutoFire = 1 Then
chkAutoFire.Checked = True
Else
chkAutoFire.Checked = False
End If
txtBurstFire.Text = BurstFire
txtAutoFire.Text = AutoFire
txtEffectiveRange.Text = EffectiveRange
txtMuzzleVelocity.Text = MuzzleVelocity
txtBaseAccuracy.Text = BaseAccuracy
txtMobility.Text = Mobility
txtDamage.Text = Damage
txtDamageFalloff.Text = DamageFalloff
txtAmmunition.Text = Ammunition
ElseIf AnimationSet = "Unarmed" Or AnimationSet = "Knife" Or AnimationSet = "Machete" Then
txtName.Text = Name
txtDescription.Text = Description
'picIcon.Image.
lblAnimationSet.Text = AnimationSet
txtSlotCount.Text = SlotsCount
txtWeight.Text = Weight
txtAPtoFire.Text = APCost
If BurstFire = 1 Then
chkBurstFire.Checked = True
Else
chkBurstFire.Checked = False
End If
If AutoFire = 1 Then
chkAutoFire.Checked = True
Else
chkAutoFire.Checked = False
End If
txtEffectiveRange.Text = EffectiveRange
txtMuzzleVelocity.Text = MuzzleVelocity
txtBaseAccuracy.Text = BaseAccuracy
txtMobility.Text = Mobility
txtDamage.Text = Damage
txtDamageFalloff.Text = DamageFalloff
txtAmmunition.Text = Nothing
ElseIf AnimationSet = "Throw" Then
Dim MaxRange As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].MaxRange").ToString)
Dim ThrowRange As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].ThrowRange").ToString)
txtName.Text = Name
txtDescription.Text = Description
'picIcon.Image.
lblAnimationSet.Text = AnimationSet
txtSlotCount.Text = SlotsCount
txtWeight.Text = Weight
txtAPtoFire.Text = APCost
If BurstFire = 1 Then
chkBurstFire.Checked = True
Else
chkBurstFire.Checked = False
End If
If AutoFire = 1 Then
chkAutoFire.Checked = True
Else
chkAutoFire.Checked = False
End If
txtEffectiveRange.Text = EffectiveRange
txtMuzzleVelocity.Text = MuzzleVelocity
txtBaseAccuracy.Text = BaseAccuracy
txtMobility.Text = Mobility
txtDamage.Text = Damage
txtDamageFalloff.Text = DamageFalloff
txtMaxRange.Text = MaxRange
txtThrowRange.Text = ThrowRange
txtAmmunition.Text = Nothing
End If
End Sub
End Class
Public Class ItemObject
Public Property Id As String
Public Property Name As String
Public Property Description As String
Public Property Icon As String
Public Property AnimationSet As String
Public Property Type As String
Public Property EquipmentClass As String
Public Property SlotsCount As Integer
Public Property Weight As Integer
Public Property AP As Integer
Public Property BurstFire As Integer
Public Property AutoFire As Integer
Public Property EffectiveRange As Double
Public Property MuzzleVelocity As Double
Public Property BaseAccuracy As Double
Public Property Mobility As Double
Public Property Damage As Integer
Public Property DamageFalloff As Double
Public Property AmmoClips As List(Of String)
Public Property Melee As Integer?
Public Property ActionClass As String
Public Property MaxRange As Integer?
Public Property ThrowRange As Integer?
Public Property RandomLootPercents As Integer?
Public Property ExplosionType As String
End Class
Public Class Items
Public Property defaultActionClass As String
Public Property ItemObjects As List(Of ItemObject)
End Class
Public Class RootObject
Public Property Items As Items
End Class
这里缺少什么?amomclips是一个数组,因此您可能应该添加[]this来获取其内容
Public Property AmmoClips As String()
Ammunition = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AmmoClips[0]").ToString)
好的,根据您最新的代码,您的
ItemObject
类中有AmmoClips
声明为列表(字符串)
。因此,在您的按钮中单击,您需要:
将变量声明为列表(字符串)
Add()
将弹药字符串添加到该列表中
将列表指定给AmmoClips
属性
此外,看起来您正在尝试将其他文本框中的字符串直接分配给在ItemObject
类中定义为Integer
和Double
的属性。这也会导致错误。您需要使用CInt
和CDbl
来转换这些情况下的值。因此,更新的按钮1\u单击代码应如下所示:
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sJsonObj As RootObject = JsonConvert.DeserializeObject(Of RootObject)(File.ReadAllText(GamePath & Gamefile))
Dim AmmoOutput As New List(Of String)
AmmoOutput.Add(txtAmmunition.Text)
sJsonObj.Items.ItemObjects.Add(New ItemObject With {
.Id = NextCount,
.Name = txtName.Text,
.Description = txtDescription.Text,
.Icon = "Icons/Weapons/M16.png",
.AnimationSet = "Rifle",
.Type = "Weapon",
.EquipmentClass = "Hands",
.SlotsCount = CInt(txtSlotCount.Text),
.Weight = CInt(txtWeight.Text),
.AP = CInt(txtAPtoFire.Text),
.BurstFire = CInt(txtBurstFire.Text),
.AutoFire = CInt(txtAutoFire.Text),
.EffectiveRange = CDbl(txtEffectiveRange.Text),
.MuzzleVelocity = CDbl(txtMuzzleVelocity.Text),
.BaseAccuracy = CDbl(txtBaseAccuracy.Text),
.Mobility = CDbl(txtMobility.Text),
.Damage = CInt(txtDamage.Text),
.DamageFalloff = CDbl(txtDamageFalloff.Text),
.AmmoClips = AmmoOutput
})
'Serialize to JSON string.
Dim settings As New JsonSerializerSettings
settings.NullValueHandling = NullValueHandling.Ignore
settings.Formatting = Formatting.Indented
Dim strAddJson As String = JsonConvert.SerializeObject(sJsonObj, settings)
' Write to file.
File.WriteAllText(GamePath & Gamefile, strAddJson)
End Sub
不管怎么说,取回还是有效的,是文字一直在折磨着我。它不断返回:无法将类型为“System.String”的对象强制转换为类型为“System.String[]您会遇到此错误,因为ammony
变量是一个包含值[“ammonclip556natox30”]
的字符串。您不能将其分配回AmmoClips
(这是一个字符串数组)--您应该将整个数组提取到一个字符串数组中,进行任何更改,然后在写回该数组时将该数组分配回AmmoClips属性。这是我正在努力解决的部分,即如何将特定项提取到字符串数组中。老实说,在这一点上,我甚至没有试图编辑它,我只是试图读它,然后写回去。同样的问题是,处理加法的按钮一点击就会爆炸。ammoclip=字面上的任何东西,字符串,变量,等等。我是否需要在ItemObjects下将AmmoClips定义为自己的类而不是属性?您是否可以编辑您的问题以显示“爆炸”的完整代码以及您遇到的错误?我看不到你的代码(除了你发布的代码片段),如果我不得不猜测你在做什么,就很难诊断出哪里出了问题。添加了所有内容,是的,这很难看,因为我一直在尝试各种事情。实际错误无法将“System.String[]”类型的对象强制转换为“System.Collections.Generic.List`1[System.String]”类型。谢谢,这非常有帮助。让我看一下,我很快会回来报告。好的,我已经根据你提供的信息更新了我的答案。