#Parsing a nested dictionary

1 messages · Page 1 of 1 (latest)

neon tusk
#

Hi! I'm attempting to load dialogue and events from a file as an alternative to making scriptableobjects however I'm having trouble parsing a nested dictionary.

There doesn't seem to be any errors surrounding the types being incompatible however I'm having a syntax error for some reason.

fast ravine
#

what is dialogue here

#

as the error says, it currently does not exist as a variable

#

(and as the error says, your missing a > in your test line)

neon tusk
#

Oh I briefly changed the var name to see any differences before taking a screenshot

#

test is suppose to be dialogue

slender parrot
#

There needs to be the same number of < as there are > on every line

fast ravine
#

the actual problem here is that dictionaries are Key, Value.

Your doing a nested one, which functionally is Key,,Key,Value

#

oh wait thats double nested too

#

cursed

slender parrot
#

A nested one is still key, value

#

It's just that the value also has a key and a value

#

And that value might also have a key and a value

fast ravine
#

oh forsure, when i say functionally i mean in how you would index into it

neon tusk
#

yeah, that's what's making it so tricky, I was trying to do Dictionary<string, dynamic> but then I got an error following that.

slender parrot
#

Also this appears to be a dictionary of a list of dictionaries of dictionaries

fast ravine
#

it's not tricky persay just a weird way to go about it + your only indexing into the first layer

fierce lark
#

FYI, JsonUtility can't deal with dictionaries

neon tusk
#

I've managed to parse jsons to dictionaries before but from Godot

fast ravine
#

dialogue["RoutineEvents"] gives you your Dictionary<string, Dictionary<string, string[]> object

neon tusk
#

I'm just wondering about an alternative method I could go about parsing the json into a dictionary

fierce lark
#

So you'd need to use a different serializer or a different intermediate type

neon tusk
#

i see

fast ravine
#

What does your data actually look like

neon tusk
#

here, this is the structure.

#

Something else I'm thinking about is instead of parsing the file into a dictionary, I could just parse it to class objects instead since that's something I've managed to do with the savefile.

fast ravine
#

Just vibe checking but are you new to c# or reasonably familiar

neon tusk
#

I've studied it and practiced it in previous projects with Unity however I'm more used to working with Godot and have not really dealt much with serializing data in Unity until recently.

fast ravine
#

I'm not crazy experienced in serialization but yeah a base class to handle those seems nice, like

DialogueInfo

string textKey;
string[] textLines;

And then you could look into alternate ways to serialize a <string, DialogueInfo> directly or just serialize the DialogueInfo's and build the dictionary at runtime when you deserialize them

glacial canyon
#

JSONUtility isn't really suitable for general purpose JSON handling, so in this case you probably want to get an actual JSON library like newtonsoft

polar dock
delicate cairn