【发布时间】:2018-11-18 15:57:36
【问题描述】:
我正在学习 Python 和 aws。
我想要的是从 JSON 响应中提取值。此代码适用于不包含日期值的 JSON 响应,但在这种情况下 response 包含 date 值。
这是我的代码:
import datetime
from datetime import date, datetime
import boto3
import json
client = boto3.client('lex-models')
response = client.get_utterances_view(
botName='CreateServicesBot',
botVersions=[
'$LATEST',
],
statusType='Missed'
)
with open('/tmp/output.json', 'w') as data:
json.dump(response,data)
with open('/tmp/output.json') as f:
data = json.load(f)
ustr=data["utteranceString"]
print ustr
我收到此错误:
TypeError: datetime.datetime(2018, 6, 7, 9, 44, 38, 146000, tzinfo=tzlocal()) is not JSON serializable
有人知道解决方案吗? 谢谢
【问题讨论】:
-
下次发帖前请使用搜索功能。
-
您好,这没有回答我的问题,我无法解决。
-
json.dumps(d, default=str) 将完成这项工作。 'default' 参数有助于将不可序列化的对象转换/转换为给定的类型/函数
标签: python json amazon-web-services datetime time