azure 函数:返回 json 对象

mas*_*kar 9 azure-functions azure-functions-core-tools

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = {"test":"jjj"}
    return func.HttpResponse(name)
Run Code Online (Sandbox Code Playgroud)

上面是我使用 python 预览的 azure 函数(V2)。如果我回来

func.HttpResponse(f"{name}") 
Run Code Online (Sandbox Code Playgroud)

它有效,但如果我返回 dict 对象,则无效。显示的错误是

Exception: TypeError: reponse is expected to be either of str, bytes, or bytearray, got dict
Run Code Online (Sandbox Code Playgroud)

请帮忙。

bra*_*nks 22

您需要使用内置的 json 库将您的字典转换为 json 字符串。 https://docs.python.org/3/library/json.html#json.dumps

然后您可以将函数的 mimetype (Content-Type) 设置为application/json. 默认情况下,Azure 函数HttpResponse返回text/plainContent-Type。

import json
import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = {"test":"jjj"}
    return func.HttpResponse(
        json.dumps(name),
        mimetype="application/json",
    )
Run Code Online (Sandbox Code Playgroud)


小智 7

更好的方法:

func.HttpResponse.mimetype = 'application/json'
func.HttpResponse.charset = 'utf-8'

return func.HttpResponse(json_object)
Run Code Online (Sandbox Code Playgroud)


Md *_*ron 1

首先,我不是Python专家。我只是想让你知道问题到底是什么。

因此,在 Azure 函数上,如果你看一下,它的返回类型似乎是IActionResult. 如果你decompile这样做,你会看到:

IActionResult IConvertToActionResult.Convert()
    {
      ActionResult result = this.Result;
      if (result != null)
        return (IActionResult) result;
      return (IActionResult) new ObjectResult((object) this.Value)
      {
        DeclaredType = typeof (TValue)
      };
    }
Run Code Online (Sandbox Code Playgroud)

因此它需要您提供一个对象,而不是字典或列表或任何类型的泛型类型。但是,如果将其转换为类似 的对象OkObjectResult,则不会遇到任何编译错误。请参阅下面的示例:

IDictionary<int, string> MyDictionary = new Dictionary<int, string>();
MyDictionary.Add(new KeyValuePair<int, string>(1, "One"));
MyDictionary.Add(new KeyValuePair<int, string>(2, "Two"));

// As we have to return IAction Type, so converting to IAction class
// using OkObjectResult we can even use OkResult
return new MyDictionary;
Run Code Online (Sandbox Code Playgroud)

上面的代码会遇到编译错误。因为它不直接支持字典或列表或任何泛型。请参阅下面的屏幕截图:

在此输入图像描述

看看错误说了什么:

在此输入图像描述

但是,如果您将字典或列表或任何泛型转换为对象类型,它肯定会解决您的问题。请参阅下面的示例:

IDictionary<int, string> MyDictionary = new Dictionary<int, string>();
MyDictionary.Add(new KeyValuePair<int, string>(1, "One"));
MyDictionary.Add(new KeyValuePair<int, string>(2, "Two"));

// So have converted MyDictionary into Object type that the compiler deserve. And no compile error encountered.
return new OkObjectResult(MyDictionary);
Run Code Online (Sandbox Code Playgroud)

请参阅下面的屏幕截图:

在此输入图像描述

注意:如果您尝试将您的对象解析func.HttpResponse(name)为对象然后最终返回,您的问题应该会解决。所以你可以尝试在你的项目()中添加对此包的引用import json,并尝试以下代码:

import json

name = {"test":"jjj"}

print(json.dumps(name))
Run Code Online (Sandbox Code Playgroud)