C
C#2y ago
Pillow

❔ CS version of python dictionary

Is there a good alternative to python's dictionaries? As in json "objects".
16 Replies
Thinker
Thinker2y ago
System.Collections.Generic.Dictionary
Pillow
PillowOP2y ago
how do I use that?
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Success
Dictionary<string, int> ages = new();
ages.Add("Rick Astley", 56);
ages.Add("Michael Stevens", 36);

Console.WriteLine(ages["Rick Astley"]);
Dictionary<string, int> ages = new();
ages.Add("Rick Astley", 56);
ages.Add("Michael Stevens", 36);

Console.WriteLine(ages["Rick Astley"]);
Console Output
56
56
Compile: 556.128ms | Execution: 32.909ms | React with ❌ to remove this embed.
Pillow
PillowOP2y ago
can i set a {"x","y"} to a field using that?
Thinker
Thinker2y ago
wdym?
Pillow
PillowOP2y ago
something like
"Rick_Astley":{
"job":"Musician",
"age":123
},
"John_Smith":{
"job":"Office worker",
"age":44
}
"Rick_Astley":{
"job":"Musician",
"age":123
},
"John_Smith":{
"job":"Office worker",
"age":44
}
Thinker
Thinker2y ago
Ah, yeah. In that case you're gonna have to use JsonSerializer. First you'll have to create a class representing a person which you can deserialize the JSON into, in this case containing a Job and Age property.
Pillow
PillowOP2y ago
ah looks like microsoft has a good page on it, thank you for your help!
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Success
using System.Text.Json;

class Person
{
public string Job { get; set; }
public int Age { get; set; }
}

string json = """
{
"Rick Astley":{
"job":"Musician",
"age":123
},
"John Smith":{
"job":"Office worker",
"age":44
}
}
""";

var people = JsonSerializer.Deserialize<Dictionary<string, Person>>(json, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
people["Rick Astley"]
using System.Text.Json;

class Person
{
public string Job { get; set; }
public int Age { get; set; }
}

string json = """
{
"Rick Astley":{
"job":"Musician",
"age":123
},
"John Smith":{
"job":"Office worker",
"age":44
}
}
""";

var people = JsonSerializer.Deserialize<Dictionary<string, Person>>(json, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
people["Rick Astley"]
Result: Person
{
"job": "Musician",
"age": 123
}
{
"job": "Musician",
"age": 123
}
Compile: 567.269ms | Execution: 64.650ms | React with ❌ to remove this embed.
Thinker
Thinker2y ago
yeah the MS docs are really good
Pillow
PillowOP2y ago
(do i have to close this now or how does it work here?)
Thinker
Thinker2y ago
Yeah you can close it using /close if you want (although idk what cathei is writing)
cathei
cathei2y ago
Dictionary<string, Person> people = new()
{
{ "Rick_Astley", new() { Job = "Musician", Age = 123 } },
{ "John_Smith", new() { Job = "Office worker", Age = 44 } }
}
Dictionary<string, Person> people = new()
{
{ "Rick_Astley", new() { Job = "Musician", Age = 123 } },
{ "John_Smith", new() { Job = "Office worker", Age = 44 } }
}
This would be equivalent syntax in C# (If you don't need to use Json)
Pillow
PillowOP2y ago
ooo fancy, thank you!
Angius
Angius2y ago
Or
Dictionary<string, Person> people = new()
{
["Rick_Astley"] = new() { Job = "Musician", Age = 123 } },
["John_Smith"] = new() { Job = "Office worker", Age = 44 } }
}
Dictionary<string, Person> people = new()
{
["Rick_Astley"] = new() { Job = "Musician", Age = 123 } },
["John_Smith"] = new() { Job = "Office worker", Age = 44 } }
}
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server