Adding new property to json object using VB.Net

两盒软妹~` 提交于 2019-12-08 06:27:56

问题


I'm trying to add a new property to an existing object from a json file. I would like to add a new profile being stored in a json file

Here is the json structure of the file.

You see, I need to go after: "profiles": { and add the stuff here, duplicate one of those profiles structure and paste it here with new data. ex. "NEW PROFILE NAME": {"name": "NEW PROFILE NAME", "lastVersionId": "VERSION ID"

Here is what I have so far, but don't know what to do with it, maybe you will understand

Imports System.IO
Imports Newtonsoft
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Module Module1

    Sub Main()
        Dim reader As New StreamReader("C:\Launcher_Profiles.json")
        Dim jsonData As String = reader.ReadToEnd

        reader.Close()

        'At this point we have the json data in a string variable, parse it and enumerate  
        'each token in the parent "objects" and if it starts with realms we will add a new  
        'MinecraftData object to a list of MinecraftData objects  
        Dim allData As JObject = JObject.Parse(jsonData)

        Dim minecraftDataList As New List(Of ProfileData)

        For Each token As JToken In allData("objects")

            Dim prop As JProperty = token

            If prop.Name.StartsWith("profiles") Then
                minecraftDataList.Add(New ProfileData With {.ProfileName = prop.Name, .ProfileVersion = prop.Value("ProfileVersion"), .Size = prop.Value("size")})
            End If

        Next

        'Now you have all of the data in the minecraftDataList  
        For Each md As ProfileData In minecraftDataList
            Console.WriteLine(md.ProfileName)
            Console.WriteLine(md.ProfileVersion)
            Console.WriteLine(md.Size)
            Console.WriteLine()
        Next

        Console.ReadLine()
    End Sub

End Module

来源:https://stackoverflow.com/questions/30690288/adding-new-property-to-json-object-using-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!