The Microsoft System.Text.Json.Serialization.JsonSerializer cannot (yet) convert JSON into dynamic objects. But good ole Newtonsoft.Json can.
The conversion is simple too:
// This is my json string
string s = "
{
"label": "MyLabel",
"values": {
"Key1": "Value1",
"Key2": "Value2",
"Key2": "Value3",
}
}";
// This is the conversion
dynamic json =
JsonConvert.DeserializeObject<ExpandoObject>(
s,
new ExpandoObjectConverter()
);
After the conversion, you can work with the dynamic object as you wish:
string label = json.label;
foreach (var value in json.values)
Console.WriteLine($"{value.Key}: {value.Value}");
MORE TO READ:
- How to serialize and deserialize (marshal and unmarshal) JSON in .NET from Microsoft
- Json.Net Documentation from newtonsoft.com
- C# Using Newtonsoft and dynamic ExpandoObject to convert one Json to another from briancaos
- Creating dynamic arrays and lists using Dynamic and ExpandoObject in C# from briancaos
- C# – Deserialize JSON to dynamic object by makolyte