There is this JSON file, I want to get all the data from it and make a dictionary with sub-dictionaries, i.e. the dictionary contains keys 0, 1, 2, and the values of the keys contain another dictionary with the values as follows
I tried to do something but an error occurs that the value cannot be converted to Godot.Variant
E 0:00:00:01:0686 void System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(System.Type): System.Text.Json.Json.JsonException: The JSON value could not be converted to Godot.Variant.
Here is my code:
using System;
using System.IO;
using System.Text.Json;
using Godot;
using Godot.Collections;
namespace Game.Data
{
public partial class SkillParameters : GodotObject
{
public SkillParameters()
{
_skillParameters = new Dictionary<string, Dictionary<string, Variant>> {};
string json_string = File.ReadAllText("Data/SkillsData.json");
_skillParameters = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, Variant>>>(json_string);
GD.Print(GetData(0, "Name"));
}
private Dictionary<string, Dictionary<string, Variant>> _skillParameters;
public Variant GetData(int key, string what)
{
return _skillParameters[Convert.ToString(key)][what];
}
public Variant GetData(string key, string what)
{
return _skillParameters[key][what];
}
public void SetData(int key, string what, Variant value)
{
_skillParameters[Convert.ToString(key)][what] = value;
}
public void SetData(string key, string what, Variant value)
{
_skillParameters[key][what] = value;
}
}
}
I have tried using JSON class from Godot and watched different tutorials, but all of them are customized for GDScript, I have also written a project using JSON in GDScript before and everything worked fine, but trying to port it to C# was not successful. The whole problem is that by using the JSON methods from godot in C#, they return a ‘Variant’ type and I can’t convert it to Dictionary<…>. If I initially give my dictionary a var or Variant type, I lose the ability to refer to it as a dictionary, so I didn’t use JSON from godot.