无法转储JSON文件,不断收到类型错误

时间:2017-11-02 04:28:50

标签: python json python-2.7 python-3.x

所以我有一个我的对象帐户的功能,一旦它创建自己它将自己记录到一个文件,但它似乎不起作用。我一直收到错误,TypeError:'getset_descriptor'类型的对象不是JSON可序列化的。提前感谢任何建议。

import os
import json
import datetime

clear = lambda: os.system("cls")
now = datetime.date
temp = 1
temp_intger = None

class Account:
    def __init__(self , account_number , account_balance , account_creation , account_firstname , account_lastname):
        self.acc_number = account_number
        self.acc_balance = account_balance
        self.acc_creation = account_creation
        self.acc_firstname = account_firstname
        self.acc_lastname = account_lastname

def account_creation():
    acc = {}
    acc["account_firstname"] = input("Please enter the first name of the account")
    acc["account_lastname"] = input("Please enter the last name of the account")
    acc["account_balance"] = float(input("Please enter the balance of the account"))
    acc["account_creation"] = now.day
    while True:
        temp = input("Please enter a 4 pin number for your account!")
        if ((os.path.isfile("%s.json" %(temp))) == True):
            continue
        else:
            break
    with open("%s.json" % (temp) , "w") as fp:
        json.dump(acc,fp)

    print ("Done!")

account_creation()

2 个答案:

答案 0 :(得分:0)

TypeError:'getset_descriptor'类型的对象不是JSON可序列化的,因为你的日期(now = datetime.date,now.day()) 您的json无法序列化您的日期对象以克服此问题,您需要将日期修改为字符串

now = str(datetime.date)

acc["account_creation"] = str(now.day)

答案 1 :(得分:0)

我相信你的麻烦就在第6行:now = datetime.date

看起来您正在尝试使用当前时间创建一个日期时间对象,但这不是您使用该行所做的事情。

我相信你想要now = datetime.datetime.now()