(Sorry my bad english.)
我昨天安装了JSON.Net(Newtonsoft)。 所以我读了文件..例子......但是我什么都做不了。
首先,这是我的json代码。
{
"profiles": {
"164o": {
"name": "164o",
"lastVersionId": "1.6.4-Forge9.11.1.965",
"launcherVisibilityOnGameClose": "keep the launcher open"
},
"BellCraft5": {
"name": "BellCraft5",
"gameDir": "C:\\Users\\HideHideHide\\AppData\\Roaming\\.custompacks\\BellCraft5",
"lastVersionId": "1.6.4-Forge9.11.1.965",
"javaArgs": "-Xmx2G -XX:PermSize\u003d128m -XX:MaxPermSize\u003d256m",
"launcherVisibilityOnGameClose": "keep the launcher open"
},
"HideHideHide": {
"name": "HideHideHide",
"lastVersionId": "1.7.9_kr"
},
"TFC": {
"name": "TFC",
"gameDir": "C:\\Users\\HideHideHide\\AppData\\Roaming\\.minecraft\\crazy",
"lastVersionId": "1.6.4-Forge9.11.1.965",
"launcherVisibilityOnGameClose": "hide launcher and re-open when game closes"
},
"Nooby": {
"name": "Nooby",
"gameDir": "C:\\Users\\HideHideHide\\AppData\\Roaming\\.custompacks\\172wepsv",
"lastVersionId": "1.7.2-Forge10.12.2.1121",
"launcherVisibilityOnGameClose": "keep the launcher open"
},
"OswinCraft": {
"name": "OswinCraft",
"gameDir": "C:\\Users\\HideHideHide\\AppData\\Roaming\\.custompacks\\OswinCraft",
"lastVersionId": "1.6.4-Forge9.11.1.965",
"launcherVisibilityOnGameClose": "keep the launcher open"
}
},
"selectedProfile": "OswinCraft",
"clientToken": "HideHideHide-18ef-4b3a-a421-HideHideHide",
"authenticationDatabase": {
"HideHideHide": {
"username": "dbcc@abcd.com",
"accessToken": "HideHideHide",
"userid": "HideHideHide",
"uuid": "HideHideHide",
"displayName": "HideHideHide"
},
"HideHideHide2": {
"username": "abcd@dbcc.com",
"accessToken": "HideHideHide",
"userid": "HideHideHide",
"uuid": "HideHideHide",
"displayName": "HideHideHide"
}
},
"selectedUser": "HideHideHide",
"launcherVersion": {
"name": "1.5",
"format": 16
}
}
是的,只是其中一个游戏设置。 我想制作一个档案。
等
"Test Profile": {
"name": "Test Profile",
"gameDir": "C:\\Test",
"lastVersionId": "Test"
}
是的,我试过
Public Class Form
Private Sub AnySub()
Dim sJsonObj As JSON_Object = JsonConvert.DeserializeObject(Of JSON_Object)(File.ReadAllText("C:\example.json"))
End Sub
End Class
Public Class JSON_Object
Public profiles As Object
Public selectedProfile As String
Public clientToken As String
Public authenticationDatabase As Object
Public selectedUser As String
Public launcherVersion As Object
End Class
但我不能。
谁能帮帮我? (如果您知道将其序列化为文件,请教我。)答案 0 :(得分:0)
将profiles
反序列化为词典,并为其添加新的配置文件。
Imports Newtonsoft.Json
Imports System.IO
Public Class Profile
Public name As String
Public gameDir As String
Public lastVersionId As String
Public javaArgs As String
Public launcherVisibilityOnGameClose As String
End Class
Public Class JSON_Object
Public profiles As Dictionary(Of String, Profile) '' Object -> Dictionary
Public selectedProfile As String
Public clientToken As String
Public authenticationDatabase As Object
Public selectedUser As String
Public launcherVersion As Object
End Class
Module Module1
Sub Main()
Dim sJsonObj As JSON_Object =
JsonConvert.DeserializeObject(Of JSON_Object)(File.ReadAllText("C:\example.json"))
' Add a new profile.
sJsonObj.profiles.Add("Test Profile", New Profile With {
.name = "Test Profile",
.gameDir = "C:\Test",
.lastVersionId = "Test"
})
' Serialize to JSON string.
Dim settings As New JsonSerializerSettings
settings.NullValueHandling = NullValueHandling.Ignore
settings.Formatting = Formatting.Indented
Dim strJson As String = JsonConvert.SerializeObject(sJsonObj, settings)
' Write to file.
File.WriteAllText("C:\your\writable\path\exampleNew.json", strJson)
End Sub
End Module