#Parsing a nested dictionary
1 messages · Page 1 of 1 (latest)
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)
Oh I briefly changed the var name to see any differences before taking a screenshot
test is suppose to be dialogue
There needs to be the same number of < as there are > on every line
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
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
oh forsure, when i say functionally i mean in how you would index into it
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.
Also this appears to be a dictionary of a list of dictionaries of dictionaries
it's not tricky persay just a weird way to go about it + your only indexing into the first layer
FYI, JsonUtility can't deal with dictionaries
I've managed to parse jsons to dictionaries before but from Godot
dialogue["RoutineEvents"] gives you your Dictionary<string, Dictionary<string, string[]> object
I'm just wondering about an alternative method I could go about parsing the json into a dictionary
Right, but JsonUtility in particular doesn't support them
So you'd need to use a different serializer or a different intermediate type
i see
What does your data actually look like
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.
Just vibe checking but are you new to c# or reasonably familiar
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.
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
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
JsonUtility is very performant though
There is also this package, which has both a high-level and low-level API, though I haven't used it myself yet
https://docs.unity3d.com/Packages/com.unity.serialization@3.1/manual/index.html
Unity's version of newtonsoft json https://docs.unity3d.com/Packages/com.unity.nuget.newtonsoft-json@3.2/manual/index.html