【问题标题】:How to return multiple values as JSON using python 3.8?如何使用 python 3.8 将多个值作为 JSON 返回?
【发布时间】:2021-03-07 23:37:26
【问题描述】:

我必须在某个变量中保存几个不同的 url,然后我必须使用 json.dumps 从 AWS Lambda 函数中返回它们。我正在尝试以下方法,但它给了我错误"errorMessage": "unhashable type: 'dict'"。代码如下。

response1 = { "statusCode": 200, "message": "Audio File uploaded successfully", "Link": some_variable1}
response2 = { "statusCode": 200, "message": "Spectrograph uploaded successfully", "Link": some_variable2}
response3 = {response1, response2}
   return {
   'statusCode': 200,
   'body': json.dumps(response3)
   }

知道如何让它工作吗?

【问题讨论】:

    标签: python json python-3.x amazon-web-services aws-lambda


    【解决方案1】:

    {response1, response2} 是一个集合文字,它要求项目是可散列的。在这种情况下,response1、response2 是不可散列的字典。

    >>> a_dictionary = {"statusCode": 200}
    >>> {a_dictionary}
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'dict'
    

    你可以使用列表或元组来代替集合,它们不需要项目是可散列的。

    >>> [a_dictionary]
    [{'statusCode': 200}]
    >>> (a_dictionary,)
    ({'statusCode': 200},)
    
    response3 = [response1, response2]  # list
    # or
    response3 = (response1, response2)  # tuple
    

    【讨论】:

      猜你喜欢
      • 2012-07-05
      • 2022-01-19
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2020-04-24
      相关资源
      最近更新 更多