Dictionary help (its confusing)

I turn a json file into a dictionary, how do I get sub data? (Getting "c") All I can do is dictionary["a"] not dictionary["a"]["b"]
{
"a": {
"b": {
"c": true
}
}
}
{
"a": {
"b": {
"c": true
}
}
}
21 Replies
🌸 morgan period 🌸
And I'm also struggling with counting? dictionary["a"].Count doesn't exist but dictionary.Count does?
Jimmacle
Jimmacle4mo ago
show code? if you're turning that json into a dictionary, it would be a Dictionary<string, Dictionary<string, bool>> but you probably want a more specifically defined model for your data
Angius
Angius4mo ago
Are you sure it's supposed to be a dictionary and not a proper class?
Anton
Anton4mo ago
is this about IConfiguration?
🌸 morgan period 🌸
public void Init() {
string data = Load_Json(dataPath);

Json jsonLoader = new Json();
Error error = jsonLoader.Parse(data);

if (error != Error.Ok) {
return;
}

Dictionary loadedData = (Dictionary)jsonLoader.Data;
}

private string Load_Json(String filePath) {
filePath = ProjectSettings.GlobalizePath(filePath);
GD.Print(filePath);

string data = null;
if (!File.Exists(filePath)) { return null;}

try {
data = File.ReadAllText(filePath);
} catch (System.Exception e) {
GD.Print(e);
}

return data;
}
public void Init() {
string data = Load_Json(dataPath);

Json jsonLoader = new Json();
Error error = jsonLoader.Parse(data);

if (error != Error.Ok) {
return;
}

Dictionary loadedData = (Dictionary)jsonLoader.Data;
}

private string Load_Json(String filePath) {
filePath = ProjectSettings.GlobalizePath(filePath);
GD.Print(filePath);

string data = null;
if (!File.Exists(filePath)) { return null;}

try {
data = File.ReadAllText(filePath);
} catch (System.Exception e) {
GD.Print(e);
}

return data;
}
I only have discord on my phone so I can't see if the syntax worked
Angius
Angius4mo ago
jsonLoader...? Is it something Godot-specific?
blueberriesiftheywerecats
The easiest way to know how could you potentially get that value is by debugging that variable
Angius
Angius4mo ago
Is Dictionary really non-generic? Or you can just drop a piece of code and disappear Sure, that's fine too
Jimmacle
Jimmacle4mo ago
i have no idea what json library this is
Angius
Angius4mo ago
Seeing how it seems to be something that has a non-generic return type, probably some Godot thing They loooooove their Variant type
🌸 morgan period 🌸
Is it godot related but csharp
Angius
Angius4mo ago
Yeah, but what exactly is this JSON deserialization? Because the usual way to deserialize JSON would be
var data = JsonSerializer.Deserialize<MyClass>(jsonString);
var data = JsonSerializer.Deserialize<MyClass>(jsonString);
not any sort of new Json() and casting the result to a non-generic Dictionary Ah, disappeared again I see Just so you know, at the pace of you dropping a sentence once every three hours it will be weeks until you get any sort of help
The Fog from Human Resources
are you trying to load a save file?
🌸 morgan period 🌸
I don't know anymore
The Fog from Human Resources
:SCgetoutofmyhead:
🌸 morgan period 🌸
I'm just trying to turn a json to dictionary using csharp because I thought it would look cleaner than a bunch of arrays
The Fog from Human Resources
You could use the default built in JSON class (it's really great) and then cast it into whatever fits your format If your JSON file contains more than just what you sent as an example it would actually make sense to create Objects / Classes for each layer
🌸 morgan period 🌸
The thing is that I wanted anyone to open it up and easily know what's going on so they could add more to it The keys makes it really easy to understand what pieces to change I'm going to try to use the JSON class like you said
The Fog from Human Resources
i mean people can always go in and change the values, changing the keys however is complicated no matter how you write you code
Angius
Angius3mo ago
Serializing/deserializing a class also produces keys btw
MODiX
MODiX3mo ago
Angius
REPL Result: Success
using System.Text.Json;

var json = """
{
"Foo": 69,
"Bar": "johny johny yes papa",
"People": [
{ "Name": "Bob", "ShoeSize": 46 },
{ "Name": "Margaret", "ShoeSize": 32 }
]
}
""";

// Data models to deserialize to
// They can be regular classes, no need to use records
record Data(int Foo, string Bar, Person[] People);
record Person(string Name, int ShoeSize);

// Deserialize
var data = JsonSerializer.Deserialize<Data>(json);

Console.WriteLine(data.Bar);
Console.WriteLine(data.People[0].Name);
Console.WriteLine(data.People[1].Name);
Console.WriteLine(data.People[0].ShoeSize);

// Serialize
var newJson = JsonSerializer.Serialize(data);

Console.WriteLine(newJson);
using System.Text.Json;

var json = """
{
"Foo": 69,
"Bar": "johny johny yes papa",
"People": [
{ "Name": "Bob", "ShoeSize": 46 },
{ "Name": "Margaret", "ShoeSize": 32 }
]
}
""";

// Data models to deserialize to
// They can be regular classes, no need to use records
record Data(int Foo, string Bar, Person[] People);
record Person(string Name, int ShoeSize);

// Deserialize
var data = JsonSerializer.Deserialize<Data>(json);

Console.WriteLine(data.Bar);
Console.WriteLine(data.People[0].Name);
Console.WriteLine(data.People[1].Name);
Console.WriteLine(data.People[0].ShoeSize);

// Serialize
var newJson = JsonSerializer.Serialize(data);

Console.WriteLine(newJson);
Console Output
johny johny yes papa
Bob
Margaret
46
{"Foo":69,"Bar":"johny johny yes papa","People":[{"Name":"Bob","ShoeSize":46},{"Name":"Margaret","ShoeSize":32}]}
johny johny yes papa
Bob
Margaret
46
{"Foo":69,"Bar":"johny johny yes papa","People":[{"Name":"Bob","ShoeSize":46},{"Name":"Margaret","ShoeSize":32}]}
Compile: 633.991ms | Execution: 81.985ms | React with ❌ to remove this embed.
Want results from more Discord servers?
Add your server