57 lines
1.1 KiB
C#
57 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Core.Configurations
|
|
{
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
public class KeyValueNester
|
|
{
|
|
private JObject _rootObject = new JObject();
|
|
|
|
public void AddKeyValue(string key, string jsonValue)
|
|
{
|
|
try
|
|
{
|
|
// Parse input JSON value
|
|
var valueObject = JsonConvert.DeserializeObject<JObject>(jsonValue);
|
|
var parts = key.Split(':');
|
|
|
|
// Start med root object eller nuværende struktur
|
|
JObject current = _rootObject;
|
|
|
|
// Gennemgå hver del af key'en
|
|
for (int i = 0; i < parts.Length - 1; i++)
|
|
{
|
|
string part = parts[i];
|
|
if (!current.ContainsKey(part))
|
|
{
|
|
current[part] = new JObject();
|
|
}
|
|
current = (JObject)current[part];
|
|
}
|
|
|
|
// Tilføj den sidste værdi
|
|
current[parts[^1]] = valueObject;
|
|
}
|
|
catch (JsonReaderException ex)
|
|
{
|
|
throw new ArgumentException("Invalid JSON value", nameof(jsonValue), ex);
|
|
}
|
|
}
|
|
|
|
public JObject GetResult()
|
|
{
|
|
return _rootObject;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_rootObject = new JObject();
|
|
}
|
|
}
|
|
}
|