I know that this question has been asked ad nauseam, but the existing answers haven’t been particularly helpful for me. The best is Deserialize JSON into C# dynamic object?, but I don’t want to create an object, I want to create a Dictionary.
I serialize my data like so, and I want to deserialize to precisely the same data structure:
public static string AugDictToJSON(Dictionary<string, List<Dictionary<string, object>>> dict)
{
return string.Join(",", dict.Select(
d => string.Format("{{ \"{0}\" : [ {1} ] }}", d.Key,
string.Join(",", d.Value.Select(i => SubAugDictToJSON(i)).ToArray())
)).ToArray());
}
public static string SubAugDictToJSON(Dictionary<string, object> dict)
{
return string.Join(",", dict.Select(
d => string.Format("{{ \"{0}\" : \"{1}\" }}", d.Key, d.Value.ToString())
).ToArray());
}
EDIT: Solution is the following (from accepted answer):
public static Dictionary<string, List<Dictionary<string, object>>> JSONToAugDict(string json)
{
return JsonConvert.DeserializeObject<Dictionary<string, List<Dictionary<string, object>>>>(json);
}
I recommend using the NuGet package ‘Newtonsoft.Json‘.
This package is used by WebAPI for serialization / deserialization and is quite fast. More information can be found here: http://james.newtonking.com/pages/json-net.aspx
Alternatively, as an IDictionary: