我需要将复杂的python对象转换为JSON,“复杂”是指包含int变量,字符串变量和2个自定义对象列表的对象。
我的Python对象的构造函数是:
def __init__(self, skills="",vid=""):
self.Skills = list([])
for skillID in skills.split("-"):
if not skillID == "":
tmpSkill = Skill()
tmpSkillObj = DBCommands.getSkill(skillID)
tmpSkill.ID = tmpSkillObj[0][0] #tmpSkillObj[0][0]
tmpSkill.Name = tmpSkillObj[0][1]
tmpSkill.isMain = True
tmpSkill.CurrentlyTesting = False
tmpSkill.isSub = False
tmpSkill.Level = 0
tmpSkill.Tested = False
tmpSkill.Score = 0
tmpSkill.Confidence = 0
tmpSkill.BestScore = 0
tmpSkill.ParentID = 0
self.Skills.append(tmpSkill)
self.AskedQuestions.append(tmpSkill)
self.Skills = list(self.Skills)
if not skills == "":
self.Skills[0].CurrentlyTesting = True #Start testing the first skill
if not vid == "":
self.VacancyID = int(vid)
self.PlayerID = 0
self.Score = float(0)
self.AskedQuestions = list([])
self.MaxLevel = 0
self.AssessmentIsFinished = False
我需要一种编码对象并对其进行解码的机制。
最佳答案
编码:
import base64
import pickle
token = base64.b64encode(pickle.dumps(token,-1))
解码:
import pickle
import base64
Obj = pickle.loads(base64.b64decode(token))