2021年9月23日 星期四

vbnet Json edit

 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

沒有留言:

張貼留言