类型错误: 不是JSON可序列化的

问题描述 投票:0回答:1

我正在尝试使用python创建AWS Lambda函数,python将从某个API接收文本并返回包含AudioStream对象的JSON。为此我使用AWS Polly。目前,我可以在我的机器上从AWS获取AudioStream,它运行正常。

import boto3,json

polly= boto3.client('polly')
text='<speak>hey there wassup </speak>'

spoken_text=polly.synthesize_speech(Text=text,OutputFormat='mp3',VoiceId='Aditi',TextType='ssml')

newdata=json.dumps(spoken_text)
# return newdata
print(type(spoken_text))

但是,当我尝试使用此代码段以JSON格式返回响应时,我收到错误。

/usr/bin/python3.5 /talker/aditi.py
Traceback (most recent call last):
  File "/talker/aditi.py", line 9, in <module>
    newdata=json.dumps(spoken_text)
  File "/usr/lib/python3.5/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.5/json/encoder.py", line 179, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <botocore.response.StreamingBody object at 0x7f81898af438> is not JSON serializable

Process finished with exit code 1

任何指导将不胜感激。 提前致谢!

注意:对于使用AWS Polly,我创建了一个特殊用户,并为他提供了AmazonPollyReadOnlyAccess和AmazonPollyFullAccess访问权限。

json amazon-web-services python-3.5 boto3 amazon-polly
1个回答
0
投票

你需要使用以下函数传递它:

newdata=bytes(json.dumps(spoken_text))

当您想要读取返回的值时,需要使用以下方法:

print(returned_value.read())
© www.soinside.com 2019 - 2024. All rights reserved.