0 votes
{
"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?

in Engine by (254 points)

1 Answer

0 votes
Best answer

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"];
by (124 points)
selected by

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.