【发布时间】:2020-08-06 07:25:35
【问题描述】:
由于客户函数错误,Lambda 执行失败,状态为 200:“十进制”类型的对象不是 JSON 可序列化的
我浏览了以下链接中的所有现有解决方案,但对我没有任何帮助。我究竟做错了什么?: Python JSON serialize a Decimal object
import json
import boto3
import decimal
client = boto3.resource('dynamodb')
table = client.Table('table')
def lambda_handler(event, context):
method = event["httpMethod"]
print(event)
if method=="POST":
return POST(event)
elif method=="DELETE":
return DELETE(event)
elif method=="GET":
return GET(event)
#the respons format
def send_respons(responseBody, statusCode):
response = {
"statusCode": statusCode,
"headers": {
"my_header": "my_value"
},
"body": json.dumps(responseBody),
"isBase64Encoded": 'false'
}
return response
def GET(event):
tab = table.scan()['Items']
ids = []
for item in tab:
ids.append({"id":item["id"], "decimalOBJ":decimal.Decimal(item["decimalOBJ"]}))
return send_respons(ids, 201)
【问题讨论】: