0 votes

Similar to https://github.com/godotengine/godot/issues/33161

I have an issue where my dictionary integer keys get converted to strings and that causes issues down the line. Is it possible to convert those parsed JSON keys from strings to integers ?

JSON:

"Chests": {
    "chest1_1": true,
    "chest1_1_contents": {
        "1": ["Gold", 14],
        "2": ["Arrows_Ammo", 27]

JSON Lookup:

loot_dic =  DataImport.save_data["Chests"][current_chest.name+str("_contents")]

print(loot_dic) = {1:[Gold, 14], 2:[Arrows_Ammo, 27]

The problem is that keys (1,2, etc) are strings and I need them to be integers ? Can this be achieved ? Any help appreciated.

in Engine by (51 points)

1 Answer

0 votes

You can store your chest content as array and during deserialization use element's index as your int key. It will be faster compared to Dictionary + iteration over keys (that are presented as array).

by (1,650 points)
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.