[Solved] Import class atributes with json [closed]


I think I know what your asking, and is that to load a class from a json file?

You can use the json set up like this to load classes.

My class file will be like this:

class Player():
    health = 0
    attack = 0
    defense = 0

My json file will be like this:

{"health": 10, "attack": 5, "defense": 3}

My load file will be like this:

import classes as c
import json

def load()
    with open('myFile.json', 'r') as pfile:
        jp = json.load(pfile)
        c.Player.health = jp['health']
        c.Player.attack = jp['attack']
        c.Player.defense = jp['defense']

This allows you to make the class have the same stats as the json file says.

Hope this helps.

3

solved Import class atributes with json [closed]