Get sub keys from a Json file?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Yaann
{
"unarmed":
        {
            "type":"melee",
            "range":0.281,
            "damage":1
        },
"knife":
        {
            "type":"melee",
            "range":0.381,
            "damage":2
        },
"pistol":
        {
            "type":"ranged",
            "calliber":"handgun",
            "firerate":1,
            "damage":1
        },
"shotgun":
        {
            "type":"ranged",
            "callier":"buckshot",
            "firerate":2.5,
            "damage":5
        }
}
file = new File();
    file.Open("res://scenes/weapons.json",File.ModeFlags.Read);
    string jSON = file.GetAsText();
    Godot.Collections.Dictionary result =(Godot.Collections.Dictionary)JSON.Parse(jSON).Result;
    GD.Print(result["unarmed"]["type"]);

I’m trying to access the subkeys {“type”, “range”,“damage”}, but the collections only register the first keys, is there any way I can access them?

:bust_in_silhouette: Reply From: nathanwfranke

When you try to get the value of a dictionary using [], it returns an object. To get further values, you need to cast each successive dictionary.

Godot.Collections.Dictionary weapons = (Godot.Collections.Dictionary)JSON.Parse(text).Result;
Godot.Collections.Dictionary weapon = (Godot.Collections.Dictionary)weapons["unarmed"];
string type = (string)weapon["type"];

Tried to do something a bit similar by parsing the array I just parsed, crashed the whole damn project lol.
Thanks a lot this one’s work!

Yaann | 2020-04-25 10:42