Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python JSON错误应为字符串或缓冲区:TypeError lambda函数失败_Python_Json_Amazon Web Services_Lambda - Fatal编程技术网

Python JSON错误应为字符串或缓冲区:TypeError lambda函数失败

Python JSON错误应为字符串或缓冲区:TypeError lambda函数失败,python,json,amazon-web-services,lambda,Python,Json,Amazon Web Services,Lambda,试图完成一个设置手机游戏的实验室。但是lambda函数抛出以下错误: 应为字符串或缓冲区:TypeError 回溯(最近一次呼叫最后一次): lambda_处理程序中的文件“/var/task/lambda_function.py”,第34行 json_data=json.loads(msg) 据我所知,它需要一个字符串,但变量msg是一个字典,它也包含list。有人能解释一下我是如何让它工作的吗?它应该是一个json.dump吗?python和编码新手,如果我没有以正确的方式提出问题,请原谅。

试图完成一个设置手机游戏的实验室。但是lambda函数抛出以下错误:

应为字符串或缓冲区:TypeError
回溯(最近一次呼叫最后一次):
lambda_处理程序中的文件“/var/task/lambda_function.py”,第34行 json_data=json.loads(msg)

据我所知,它需要一个字符串,但变量msg是一个字典,它也包含list。有人能解释一下我是如何让它工作的吗?它应该是一个json.dump吗?python和编码新手,如果我没有以正确的方式提出问题,请原谅。代码如下。提前谢谢

def lambda_handler(event, context):
global client
print(event)
# check the receiver's queue url
if client == None:
    client = boto3.resource('sqs')
records = event['Records'][0]
sns_data = records['Sns']
msg = sns_data['Message']
print(msg)
json_data = json.loads(msg)
type_of_msg = json_data['type']
sender = json_data['sender']
receiver = json_data['receiver']
amount = json_data['amount']
# queue_name = get_queue_name_by_account_id(receiver)
queue_name = USER_POOL_ID + "_" + receiver
# enqueue the message
queue = client.get_queue_by_name(QueueName=queue_name)
msg = {
    "type": type_of_msg,
    "amount": amount
}
res = queue.send_message(MessageBody=json.dumps(msg))
print(res)
return json_data['receiver']

json.load需要字符串或缓冲区,而不是json

msg已经是json,不需要执行json.loads

下面是一个工作示例

import boto3
import json

def lambda_handler(event, context):
    client = None
    USER_POOL_ID = 'xxxxx'
    print(event)
    # check the receiver's queue url
    if client == None:
        client = boto3.resource('sqs')
    records = event['Records'][0]
    sns_data = records['Sns']
    msg = sns_data['Message']
    print(msg)
    json_data = msg
    type_of_msg = json_data['type']
    sender = json_data['sender']
    receiver = json_data['receiver']
    amount = json_data['amount']
    # queue_name = get_queue_name_by_account_id(receiver)
    queue_name = USER_POOL_ID + "_" + receiver
    #enqueue the message
    queue = client.get_queue_by_name(QueueName=queue_name)
    msg = {
        "type": type_of_msg,
        "amount": amount
    }
    res = queue.send_message(MessageBody=json.dumps(msg))
    print(res)
    return json_data['receiver']
lambda的样本测试事件:

{
  "Records": [
      {"Sns": {"Message": {"type": "a","sender": "b","receiver": "c","amount": "d"}}}
  ]
}

希望这有帮助

请编辑上面的示例代码以获得正确的缩进。他刚才说那是一本字典。你应该使用
json.dumps