相关疑难解决方法(0)

“Decimal”类型的对象不可 JSON 序列化

由于客户函数错误,Lambda 执行失败,状态为 200:“Decimal”类型的对象不可 JSON 序列化

我在以下链接中浏览了所有现有的解决方案,但没有任何对我有用。我做错了什么?: Python JSON 序列化 Decimal 对象

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 …
Run Code Online (Sandbox Code Playgroud)

python json

13
推荐指数
3
解决办法
4万
查看次数

标签 统计

json ×1

python ×1