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

Answer from James Newton-King on Stack Overflow
Discussions

How do I convert a dictionary to a JSON String in C#? - Stack Overflow
I want to convert my Dictionary to JSON string. Does anyone know how to achieve this in C#? More on stackoverflow.com
🌐 stackoverflow.com
C# - How to get data as a dictionary from JSON file
Godot Version 4.2.1 Mono Question There is this JSON file, I want to get all the data from it and make a dictionary with sub-dictionaries, i.e. the dictionary contains keys 0, 1, 2, and the values of the keys contain ano… More on forum.godotengine.org
🌐 forum.godotengine.org
1
0
March 17, 2024
C# JSON System.Text.Json - How to extract an object from a dictionary or List<Dictionary<string, object>>(json) ?
It is probably better to use specific objects instead of Dictionary. It is possible to define a helper converter class that interprets strings as numbers or vice versa. 1 comment Show comments for this answer Report a concern ... Hi , thanks a lot for your answer, it works fine indeed. I couldn't figure out that FirstOrDefault method. To be fair, I would have liked/preferred to extract the objects and assign their values directly from the JSON ... More on learn.microsoft.com
🌐 learn.microsoft.com
1
0
March 18, 2023
json string to dictionary - Unity Engine - Unity Discussions
I have a simple json string {“1”:10,“2”:25,“3”:1,“4”:124,“7”:567} I have a Dictionary Dictionary Gameitems = new Dictionary (); What is the best way to get the json string into the dictionary? Is there a simple json parser that will do it or will i have to parse the json ... More on discussions.unity.com
🌐 discussions.unity.com
0
April 13, 2020
🌐
Reddit
reddit.com › r/csharp › json to c# dictionary questions
r/csharp on Reddit: JSON to C# Dictionary questions
October 29, 2019 -

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,

Top answer
1 of 4
2
Default serialization will take a standard json object into a dictionary Ex: this will serialize properly public class DataModel { public Dictionary Values { get; set; } } { "values": { "name": "bob", "skill": "none" } } Tldr; although a dictionary is a collection type in .net, having unique keys makes it suitable as a handler for a loosely coupled JSON objects, so deserialization allows that to happen
2 of 4
1
Not the most elegant solution, I'll grant you... but your model can be serialized like this: // usings/references using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; // class models [DataContract] class MyObject { [DataMember] public List Authorized { get; set; } [DataMember(Name = "Building Info")] public List BuildingInfo { get; set; } } [DataContract] class Authorized { [DataMember] public string Key { get; set; } [DataMember(Name = "Authorized_Buildings")] public List AuthorizedBuildings { get; set; } } [DataContract] class BuildingInfo { [DataMember] public string Key { get; set; } [DataMember] public int Floors { get; set; } [DataMember] public string Exits { get; set; } } // implementation code string json = "...your json..."; MyObject obj = new MyObject(); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) { var serializer = new DataContractJsonSerializer(obj.GetType()); obj = serializer.ReadObject(stream) as MyObject; stream.Close(); } // "obj" now holds the model data deserialized. // All the List types in the model classes could be arrays... the serializer won't care. Note that you have a typo in Joe's "Authorized_Buildings"... there's a space in the name. The MyObject class is just a placeholder because your json doesn't specify a contract name. There are many other ways to serialize/deserialize json data. This is just one.
🌐
DEV Community
dev.to › thebuzzsaw › converting-dictionaries-in-system-text-json-474b
Converting Dictionaries in System.Text.Json - DEV Community
February 26, 2020 - I'm generally not OK with having to convert JSON to a string dictionary first and then to a non-string dictionary as a second step. It seems I am not alone in experiencing frustration at this. However, as much as I love to sit and complain about others' hard work, I decided to just do something about it. The documentation for creating ...
🌐
Newtonsoft
newtonsoft.com › json › help › html › DeserializeDictionary.htm
Deserialize a Dictionary
string json = @"{ 'href': '/account/login.aspx', 'target': '_blank' }"; Dictionary<string, string> htmlAttributes = JsonConvert.DeserializeObject<Dictionary<string, string>>(json); Console.WriteLine(htmlAttributes["href"]); // /account/login.aspx Console.WriteLine(htmlAttributes["target"]); // _blank
Find elsewhere
🌐
Unity
discussions.unity.com › unity engine
json string to dictionary - Unity Engine - Unity Discussions
April 13, 2020 - I have a simple json string {“1”:10,“2”:25,“3”:1,“4”:124,“7”:567} I have a Dictionary Dictionary<int, int> Gameitems = new Dictionary<int, int>(); What is the best way to get the json string into the dictionary? Is t…
🌐
UiPath Community
forum.uipath.com › help
How to load json file into dictionary - Help - UiPath Community Forum
April 20, 2019 - Hi Everyone. Can anyone please tell me how to Load a JSON file into a dictionary. I have done up to deserialize JSON after that I took For each activity from there onwards I was Stuck. Thanks in advance Regards, CRN…
🌐
C-sharptutorial
c-sharptutorial.com › csharparticles › how-to-convert-json-object-to-dictionary-in-csharp
[SOLVED] - Convert Json to Dictionary in C#
JsonConvert belongs to the Newtonsoft.Json namespace. ... The following namespace should be used after installation. ... Dictionary<string,string>? jsonResult = JsonConvert.DeserializeObject<Dictionary<string,string>>(json == null ?
🌐
Google Groups
groups.google.com › g › restsharp › c › 38x3tR1KztA
How to deserialize JSON to Dictionary<string, class>, where keys are unknown prior?
I can see the JSON in the response, so the request went through just fine. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... You can use Json2C# which convert a json response to a data class.
🌐
Automation Anywhere
docs.automationanywhere.com › bundle › enterprise-v2019 › page › convert-json-to-dictionary.html
Convert JSON to Dictionary
This action enables you to extract JSON data to a dictionary variable to help read or process different nodes individually. Supports dictionary variables to store nested key-value pairs.
🌐
JD Bots
jd-bots.com › 2023 › 02 › 17 › convert-dictionary-to-json-object-in-c-example-code
Convert Dictionary to JSON Object in .NET C# | Example Code
February 17, 2023 - Note that you’ll need to include the using Newtonsoft.Json; statement at the top of your file in order to use the JsonConvert class. If you run the code I provided, the output will be a JSON string representation of the dict dictionary, which ...
🌐
Medium
akarshseggemu.medium.com › objective-c-how-to-convert-a-dictionary-to-json-string-752e410fead1
Objective-C: How to convert a dictionary to JSON string? | by Akarsh Seggemu, M.Sc. | Medium
June 25, 2022 - Here is an example on how to convert dictionary into JSON string, #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Akarsh", @"name", nil]; NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"Value of json string %@", jsonString); return 0; }
🌐
Code Maze
code-maze.com › home › how to serialize a dictionary to json in c#
How to Serialize a Dictionary to JSON in C# - Code Maze
May 17, 2023 - We just need to create a custom converter and add it to our serializer options declaration: public sealed class SystemJsonCustomerInvoiceConverter : JsonConverter<Dictionary<Customer, List<Invoice>>> { public override void Write(Utf8JsonWriter writer, Dictionary<Customer, List<Invoice>> value, JsonSerializerOptions options) { writer.WriteStartObject(); foreach (var (customer, invoices) in value) { writer.WritePropertyName($"Customer-{customer.CustomerId:N}"); writer.WriteStartObject(); WriteCustomerProperties(writer, customer); WriteInvoices(writer, invoices); writer.WriteEndObject(); } writer.WriteEndObject(); } // Omitted for brevity }
🌐
Reddit
reddit.com › r/learnpython › convert json list to dictionary
r/learnpython on Reddit: convert JSON list to dictionary
January 13, 2024 -

I must first preface this with the fact that I’m extremely new to python. Like just started learning it a little over a week ago.
I have been racking my brain over how to convert a json object I opened and loaded into a dictionary from a list so I can use the get() function nested within a for loop to do a student ID comparison from another json file (key name in that file is just ID).
Below is the command I’m trying to load the json file:
With open(‘file.json’) as x: object=json.load(x)
When I print(type(object)), it shows up as class list.
Here’s a sample of what the json looks like:
[

{

“Name”: “Steel”,

“StudentID”: 3458274

“Tuition”: 24.99

},

{

“Name”: “Joe”,

“StudentID”: 5927592

“Tuition”: 14.99

}

]
HELP! Thank you!