我正在尝试获取Zapier中提供给我的一些Stripe数据,并将两个纪元时间转换为人类可读的日期,以便可以轻松读取订阅期。直到几天前,下面的确切代码以前都可以正常工作,现在我收到以下错误:
Traceback (most recent call last):
File "/usr/lib64/python2.7/json/__init__.py", line 251, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/var/runtime/awslambda/bootstrap.py", line 110, in decimal_serializer
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: time.struct_time(tm_year=2018, tm_mon=10, tm_mday=22, tm_hour=14, tm_min=9, tm_sec=58, tm_wday=0, tm_yday=295, tm_isdst=0) is not JSON serializable
这是以前的工作代码:
import ast
import json
import time
data = "amount: 1000
currency: usd
description: None
discountable: True
id: sub_DfoojqxyubarmQ
livemode: True
metadata: {}
object: line_item
period: {u'start': 1540217398, u'end': 1542895798}
plan: {u'active': True, u'product': u'prod_fooj56ZAbarmaU',
u'transform_usage': None, u'name': u'Subscription - Monthly',
u'aggregate_usage': None, u'created': 1532982748, u'tiers_mode': None,
u'interval': u'month', u'tiers': None, u'object': u'plan', u'id':
u'plan_DKMmOfooQEbars', u'currency': u'usd', u'amount': 1000,
u'interval_count': 1, u'trial_period_days': None, u'livemode': True,
u'usage_type': u'licensed', u'metadata': {u'qb_sub_product': u'8'},
u'nickname': u'Subscription - Monthly', u'statement_descriptor':
u’SUBSCRIPTION’, u'billing_scheme': u'per_unit'}
proration: False
quantity: 1
subscription: None
subscription_item: si_DpfooWbarKUVIh
type: subscription"
#retrieve period data
period_begin = data.find('period:') + 8
period_end = data.find('plan:')
#convert period to JSON
period = ast.literal_eval(data[period_begin:period_end])
json_period = json.dumps(period)
json_period = json.loads(json_period)
#retrieve plan data
plan_begin = data.find('plan:') + 6
plan_end = data.rfind('}') + 1
#convert plan to JSON
plan = ast.literal_eval(data[plan_begin:plan_end])
json_plan = json.dumps(plan)
json_plan = json.loads(json_plan)
#period_start and period_end to human readable format
period_start = time.strftime("%-m/%-d/%Y"), time.localtime(json_period['start'])
period_end = time.strftime("%-m/%-d/%Y", time.localtime(json_period['end']))
output = [{'json_plan': json_plan, 'json_period': json_period, 'period_start': period_start, 'period_end': period_end}]
答案 0 :(得分:1)
您有错字。括号过早yarn -v
结束了。您的代码所做的是生成一个2值元组:将time.strftime("%-m/%-d/%Y")
设置为默认的strftime格式,并从纪元开始使用now()
。
time.localtime