I'm new to working with JSON in Python. I have a program where I generate 10 characters (objects) that have several properties. I'm write these to a file using the Python JSON library:
def saveCharsToFile(gameChars):
# Using a WITH operator for easy readability. creating and opening a file
# in write mode
with open("characterFile.json", "w") as write_file:
# using the json.dump function to take the objects in the gameChars
# list and serialise it into JSON
json.dump([x.__dict__ for x in gameChars], write_file)
# Tells the User that the file has been written
print("File 'characterFile.json' has been written to...")
Here is an example of one of the types of classes that generate a character:
# A subclass for creating a Wizard character, inherits the Character class
# and adds the power, sAttackPwr, and speed properties
class wizard(character):
def __init__(self, CharPower, CharSAttackPwr, CharSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "W", 100)
self.power = CharPower
self.sAttackPwr = CharSAttackPwr
self.speed = CharSpeed
# Base Class for creating an RPG character
class character:
# __init__ method, creates the name, type, health properties
def __init__(self, charType, charHealth):
self.name = rndName()
self.type = charType
self.health = charHealth
Here is the contents of a file saved using the saveCharsToFile:
[{"name": "ing low fu ", "type": "B", "health": 100, "power": 60, "sAttackPwr": 10, "speed": 60}, {"name": "en ar da ", "type": "W", "health": 100, "power": 50, "sAttackPwr": 70, "speed": 30}, {"name": "ar fu fu ", "type": "B", "health": 100, "power": 70, "sAttackPwr": 20, "speed": 50}, {"name": "da low en ", "type": "W", "health": 100, "power": 50, "sAttackPwr": 70, "speed": 30}, {"name": "da fu cha ", "type": "D", "health": 100, "power": 90, "sAttackPwr": 40, "speed": 50}, {"name": "da ing el ", "type": "W", "health": 100, "power": 50, "sAttackPwr": 70, "speed": 30}, {"name": "cha kar low ", "type": "D", "health": 100, "power": 90, "sAttackPwr": 40, "speed": 50}, {"name": "ar da el ", "type": "D", "health": 100, "power": 90, "sAttackPwr": 40, "speed": 50}, {"name": "da da ant ", "type": "B", "health": 100, "power": 60, "sAttackPwr": 10, "speed": 60}, {"name": "el ing kar ", "type": "B", "health": 100, "power": 70, "sAttackPwr": 20, "speed": 50}]
I'd like to be able to read this back into the program, as a list, preferably instantiating them as objects with the values for the character properties in that list above.
This is the current way I'm reading the file back into the program:
def openCharsToFile(gameChars):
with open("characterFile.json") as read_file:
y = json.load(read_file)
for x in y:
gameChars.insert[x, y]
for z in gameChars:
print(z.getStats())
But when I run this program, I get the following error:
Traceback (most recent call last):
File "rpgProblemSolving.py", line 342, in <module>
main()
File "rpgProblemSolving.py", line 338, in main
openCharsToFile(gameChars)
File "rpgProblemSolving.py", line 305, in openCharsToFile
gameChars.insert[x, y]
TypeError: 'builtin_function_or_method' object is not subscriptable
If anyone has some suggestions, It would be most appreciated thanks