[英]Deserialize the JSON string into object in Python
我編寫了這段代碼,以將對象保存並加載到JSON中。 訪問屬性時遇到了這個錯誤:
我使用python json類來序列化對象並反序列化它。
AttributeError:“ dict”對象沒有屬性“ weeknumber”
# -*- coding: utf-8 -*-
import pickle
import pprint
from datetime import date
import datetime
import decimal
import json
class Jsonable(object):
def __iter__(self):
for attr, value in self.__dict__.iteritems():
if isinstance(value, datetime.datetime):
iso = value.isoformat()
yield attr, iso
elif isinstance(value, decimal.Decimal):
yield attr, str(value)
elif(hasattr(value, '__iter__')):
if(hasattr(value, 'pop')):
a = []
for subval in value:
if(hasattr(subval, '__iter__')):
a.append(dict(subval))
else:
a.append(subval)
yield attr, a
else:
yield attr, dict(value)
else:
yield attr, value
class MyCalendar(Jsonable):
year = 0
weeks = {}
def __init__(self, _year,_weeks):
self.year = _year
self.weeks = _weeks;
class MyWeek(Jsonable):
weeknumber = 0
days = {}
def __init__(self, _week,_days):
self.weeknumber = _week
self.days = _days
class MyDay(Jsonable):
daynumber = 0
courses = {}
def __init__(self, _day,_courses):
self.daynumber = _day
self.courses = _courses
class MyCourse(Jsonable):
id = 0
title = ''
def __init__(self, _id,_title):
self.id = _id
self.title = _title
def init():
_myc = MyCalendar(date.today().year,[]);
for wi in range(0, 52):
_week = MyWeek(wi,[])
for di in range(0, 5):
_day = MyDay(di,[])
for ci in range(0, 4):
_course = MyCourse(ci,"empty")
_day.courses.append(_course)
_week.days.append(_day)
_myc.weeks.append(_week)
return _myc
def save(_t):
with open('data2.txt', 'w') as outfile:
json.dump(dict(_t), outfile)
def load():
_t = init();
with open('data2.txt', 'r') as infile:
_t.__dict__ = json.loads(infile.read())
return _t
def setCourse(_t,_weeknumnber,_dayumber,_course,_title):
_t.weeks[_weeknumnber].days[_dayumber].courses[_course].title = _title
return _t
_mycal = init();
setCourse(_mycal,1,2,3,"Python");
save(_mycal)
_mycal = load()
pprint.pprint( _mycal.weeks[1].weeknumber)
您的代碼本質上是正確的,但是您要混合很多方面,並以丑陋的方式解析JSON,從而為您提供當前結果,您無法直接訪問屬性,但是將創建的對象視為字典,並且需要通過密鑰訪問它。 為了實現自己的方式並保持屬性不變,您需要為類創建自己的解析器。 我已經在我的工作區中快速復制了您的代碼:
import json
class MyCalendar:
def __init__(self, _year, _weeks):
self.year = _year
self.weeks = _weeks
def to_JSON(self):
# Dumps the JSON correctly, passing over the __dict__ of the class to assmble it.
return json.dumps(self, default=lambda o: o.__dict__)
class MyWeek:
def __init__(self, _week, _days):
self.weeknumber = _week
self.days = _days
class MyDay:
def __init__(self, _day, _courses):
self.daynumber = _day
self.courses = _courses
class MyCourse:
def __init__(self, _id, _title):
self.id = _id
self.title = _title
def json_calendar(dct):
year = dct['year'] # year, for quick reference.
weeks = {} # our weeks, as a dict.
for week in dct['weeks']: # iterate through the available weeks in the given dct.
days = {} # our days, as a dict.
for day in dct['weeks'][week]['days']: # iterate through the available days in the week.
courses = {} # our courses, as a dict.
for course in dct['weeks'][week]['days'][day]['courses']: # iterate through the available courses in the day.
courseId = int(course) # course id
courseTitle = dct['weeks'][week]['days'][day]['courses'][course]['title'] # course title
courses[courseId] = MyCourse(courseId, courseTitle) # Assemble our courses into the courses dict.
days[int(day)] = MyDay(day, courses) # Assemble our days into the day dict.
weeks[int(week)] = MyWeek(week, days) # Assemble our weeks into the weeks dict.
return MyCalendar(year, weeks) # Assemble and return our calendar.
現在您可以簡單地創建日歷,現在要轉儲日歷時,只需執行_mycal.To_JSON()
並從JSON獲取日歷對象,則需要執行_mycal = json_calendar(json_data)
,您可以由於我們在特定的解析器中對其進行了解析,因此可以毫無問題地訪問屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.