VB.Net简单Json编辑 [英] VB.Net Simple Json Edit
本文介绍了VB.Net简单Json编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(Sorry my bad english.)
我昨天安装了JSON.Net(Newtonsoft). 所以我看了文档..例子..但是我无能为力.
I installed JSON.Net(Newtonsoft) yesterday. so i read documents.. examples.. but i cant do anything.
首先,这是我的json代码.
first, here is my json code.
{
"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
}
}
是的,这只是游戏设置之一. 我只想做一个配置文件.
Yeah, just one of game settings. i want just make one profiles.
喜欢
"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
但是我不能.
有人可以帮助我吗? (如果您知道将其序列化为文件,请教我.)
anyone can help me? (If you know about serialize it as a file, please teach me.)
推荐答案
将profiles
反序列化为字典,然后向其中添加新的配置文件.
Deserialize profiles
as a Dictionary, and add a new profile to it.
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
这篇关于VB.Net简单Json编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文