How do I get a dictionary from a JSONParseResult?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kitfox

I’m porting something I originally wrote using GDScript to c#. I’m trying to figure out how to get a Dictionary from JSONParseResult.Result. I’m calling JSON.Parse() from inside a method and I want the method to return a dictionary of the data loaded.

The data appears to be there since I can see it in the debugger - however, I cannot seem cast it to a Dictionary object. I’ve tried casting JSONParseResult.Result to Dictionary<object, object> and Dictionary<string, object>, but each time it just results in null.

How I I return a dictionary from this method?

private Dictionary<object, object> parseTileMetadata()
    HexMap map = new HexMap();

    File f = new File();
    f.Open(_tileMeta, File.ModeFlags.Read);
    JSONParseResult res = JSON.Parse(f.GetAsText());
    var data = res.Result;


    var foo = res.Result as Dictionary<object, object>;

    return foo;

I know, this probably doesn’t quite answer your question, but I stumbled on it yesterday and it also treats JSON, dictionaries and getting data into a Godot project. Maybe it shows an alternative.

ChristianSF | 2020-08-02 06:39

Landroval | 2023-02-12 16:26

:bust_in_silhouette: Reply From: kitfox

Looks like my problem was that the compiler thought that my Dictionary was from System.Collections.Generic rather than Godot.Collections. Once I specified that explicitly, I was able to read it.

how did you specify it? i got the same issue.

Sha6er | 2020-09-25 10:49

simply put Godot.Collections.Dictionary instead of just Dictionary

jgee_23_ | 2022-08-20 18:48