Json.NET does this...
string json = @"{""key1"":""value1"",""key2"":""value2""}";
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
More examples: Serializing Collections with Json.NET
I did discover .NET has a built in way to cast the JSON string into a Dictionary<String, Object> via the System.Web.Script.Serialization.JavaScriptSerializer type in the 3.5 System.Web.Extensions assembly. Use the method DeserializeObject(String).
I stumbled upon this when doing an ajax post (via jquery) of content type 'application/json' to a static .net Page Method and saw that the method (which had a single parameter of type Object) magically received this Dictionary.
How do I convert a dictionary to a JSON String in C#? - Stack Overflow
C# - How to get data as a dictionary from JSON file
json string to dictionary - Unity Engine - Unity Discussions
C# JSON System.Text.Json - How to extract an object from a dictionary or List<Dictionary<string, object>>(json) ?
Videos
I don't quite understand a couple things. I found a bunch of code on how to deserialize JSON to a C# dictionary, but nothing that quite exactly matched what I wanted to do.
So I have some JSON that looks like this:
{
"Authorized" : [
{"Key" : "John", "Authorized_Buildings" : ["Building 1", "Building 3", "Building 5"]},
{"Key" : "Bob", "Authorized_Buildings" : ["Building 1", "Building 5"]},
{"Key" : "Joe", "Authorized _Buildings" : ["Building 2", "Building 3"]}
],
"Building Info" : [
{"Key" : "One", "Floors" : 4, "Exits" : "Multiple"},
{"Key" : "Two", "Floors" : 3, "Exits" : "One"}
]
}I would like to deserialize each of these JSON "Objects" into two different Dictionaries<string, Authorized> and Dictionary <string, buildingInfo>.
My issues are how do I only send the authorized objects to that class and the Building Info objects to that Dictionary? createAuthorizedDictionaryFromJSON(jo["Authorized"].ToString());
Second, how do I force the dictionary to use what I want as the key to the custom object? So for Building Info, I want two properties in the class. A floor int and an Exits string.
I appreciate any help.
Cheers,
This answer mentions Json.NET but stops short of telling you how you can use Json.NET to serialize a dictionary:
return JsonConvert.SerializeObject( myDictionary );
As opposed to JavaScriptSerializer, myDictionary does not have to be a dictionary of type <string, string> for JsonConvert to work.
Serializing data structures containing only numeric or boolean values is fairly straightforward. If you don't have much to serialize, you can write a method for your specific type.
For a Dictionary<int, List<int>> as you have specified, you can use Linq:
string MyDictionaryToJson(Dictionary<int, List<int>> dict)
{
var entries = dict.Select(d =>
string.Format("\"{0}\": [{1}]", d.Key, string.Join(",", d.Value)));
return "{" + string.Join(",", entries) + "}";
}
But, if you are serializing several different classes, or more complex data structures, or especially if your data contains string values, you would be better off using a reputable JSON library that already knows how to handle things like escape characters and line breaks. Json.NET is a popular option.