I'm new to JSON.Net and haven't been able to figure out how to get a nested object of my JSON as a JObject. I am using a file to store my JSON objects, and then write the changes I made back to the file.
Background
I am trying to make an app where the user selects a folder on their computer, and the app enumerates that folder and all of its files into a JSON object. The user can then select any of those files and set values of that file to be saved to the JSON file. So far I have made the function to generate the directory structure/contained files into JSON and save it to a file, but I am stuck at trying to then access a nested object of the JSON in order to set a property's value.
My code so far
The code used to generate the JSON and store it into the file is:
Private Sub btnAddFolder_Click(sender As Object, e As EventArgs) Handles btnAddFolder.Click
'Create JObject from JSON File
Dim json = File.ReadAllText(jsonFile)
Dim obj As New JObject
If json <> "" Then
obj = JObject.Parse(json)
End If
'Add directory as a nested object of JSON
' - Add all of files in directory as nested objects of the directory object
If obj.Property(txtDirectory.Text) Is Nothing Then
'Object to store all files as nested objects
Dim files As New JObject
'Object to store all properties of a file object
Dim fileInfo As New JObject
'Variables to store properties for fileInfo
Dim stringProp As String = "default"
Dim arrProp() As String = {"default"}
Dim jArrProp As JArray = JArray.FromObject(arrProp)
'Add properties to objcet
fileInfo.Add("var1", stringProp)
fileInfo.Add("var2", jArrProp)
'Add object of properties to each file object
For Each file As String In Directory.GetFiles(txtDirectory.Text)
files.Add(file, fileInfo)
Next
'Add each file object to directory object
obj.Add(txtDirectory.Text, files)
End If
'Write JSON to file
File.WriteAllText(jsonFile, JsonConvert.SerializeObject(obj, 1))
End Sub
The generated file/JSON looks like:
{
"Directory1": {
"File1": {
"var1": "default",
"var2": ["default"]
},
"File2": {
"var1": "default",
"var2": ["default"]
}
},
"Directory2": {
"File1": {
"var1": "default",
"var2": ["default"]
},
"File2": {
"var1": "default",
"var2": ["default"]
}
}
}
What I need next
The code above is called when a Button labeled Add Folder is clicked.
Another Button, labeled Select File and Set Data, prompts Users to select any file from the directory and input values that will be stored in var1 and var2 of that file.
For example, when the Select File and Set Data Button is clicked, a User selects File1, then writes "This file is a picture from my 2012 vacation to the islands." in a TextBox. I need my code to set var1 of obj.Directory1.File1 to the string value specified.
This is where I am stumped. I do not know how to use JSON.net to access a nested object in order to set a value of one of its properties.
I know that a JObject has a .Property().Value() method that can be used to set or get the value of a property, but since my intended JSON object is nested inside the main JSON object, I cannot figure out how to get it as a JObject in order to access that method. JObject.Item() returns a JToken which does not have a .Property().Value() method.