JSON如下:
"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
我在这里缺少什么?
答案 0 :(得分:0)
AmmoClips是一个数组,所以你应该添加[]这个来获取它的内容
Public Property AmmoClips As String()
Ammunition = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AmmoClips[0]").ToString)
答案 1 :(得分:0)
好的,根据您的最新代码,您AmmoClips
课程中的List(Of String)
声明为ItemObject
。因此,在Button1_Click
中,您需要:
List(Of String)
Add()
您的弹药字符串到该列表AmmoClips
属性此外,您似乎正在尝试直接将其他TextBox中的字符串分配给Integer
类中定义为Double
和ItemObject
的属性。这也导致错误。在这些情况下,您需要使用CInt
和CDbl
来转换值。因此,更新的Button1_Click
代码应如下所示:
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