Imports System.Collections.Generic
Imports Newtonsoft.Json.Linq
Dim d As New Dictionary(Of String, String)
d.Add("test.nested", "value")
d.Add("test.nested2", "value2")
d.Add("test.nested3.moreNested.evenMoreNested", "value3")
Dim output As String = JsonHelper.DictionaryToJson(d)
Console.WriteLine(output)
Public Shared Function DictionaryToJson(dict As Dictionary(Of String, String)) As String
Dim root As New JObject()
For Each kvp As KeyValuePair(Of String, String) In dict
FindOrAdd(root, kvp.Key, kvp.Value)
Private Shared Function FindOrAdd(parent As JObject, key As String, value As String) As JObject
If key Is Nothing Then Return parent
Dim i As Integer = key.IndexOf(".")
Dim obj As JObject = FindOrAdd(parent, key.Substring(0, i), Nothing)
Return FindOrAdd(obj, key.Substring(i + 1), value)
Dim prop As JProperty = parent.Property(key)
Else If prop.Value.Type = JTokenType.Object
child = DirectCast(prop.Value, JObject)
Throw New JsonException("The key """ + parent.Path + "." + key + """ already has a value of """ + prop.Value.ToString() + """.")
parent.Add(key, New JValue(value))
Throw New JsonException("The key """ + parent.Path + "." + key + """ already has a value of """ + prop.Value.ToString() + """.")