首页 > 解决方案 > 如何从 azure 函数获取 json 对象的输出?

问题描述

我已经编写了一个 azure 函数,它将数据返回为 json 字符串格式,但我希望将数据转换为 json 对象,以便我可以直接使用该数组输出来进入逻辑应用程序的下一步。

天蓝色功能代码 -

        composeMessage = "{\"__metadata\": {\"id\": "+obj.id+",\"uri\": "+obj.uri+",\"dateForSystem\": "+obj.dateForSystem + ",\"timeForSystem\": "+obj.timeForSystem + "}";
        composeMessageList.Add(composeMessage);
        outputDerivedTableKey = string.Empty;
        startIndex = 0;
    }
     var jsonToReturn = JsonConvert.SerializeObject(composeMessageList);
    return new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")

得到像 -

[
  "{\"__metadata\": {\"id\": ,\"uri\": ,\"type\": },\"dateForSystem\": 2019-05-17,\"timeForSystem\": 13:15:51}",
  "{\"__metadata\": {\"id\": ,\"uri\": ,\"type\": },\"dateForSystem\": 2019-05-17,\"timeForSystem\": 13:15:51}",
  "{\"__metadata\": {\"id\": ,\"uri\": ,\"type\": },\"dateForSystem\": 2019-05-17,\"timeForSystem\": 13:15:51}",
]

但是我无法将此数组传递给逻辑应用程序中的 foreach 我正在从 azure 函数中排除如下输出格式 -

[
  {
    "__metadata": {
      "id": "",
      "uri": "",
      "type": ""
    },
    "dateForSystem": "2019-05-17",
    "timeForSystem": "13:15:51"
  },
   {
    "__metadata": {
      "id": "",
      "uri": "",
      "type": ""
    },
    "dateForSystem": "2019-05-17",
    "timeForSystem": "13:15:51"
  },
   {
    "__metadata": {
      "id": "",
      "uri": "",
      "type": ""
    },
    "dateForSystem": "2019-05-17",
    "timeForSystem": "13:15:51"
  },
]

如何从 azure 函数实现这种格式输出?

或者如何将其格式化为逻辑应用程序?

标签: c#azureazure-functionsazure-logic-apps

解决方案


问题是序列化对象是一个字符串列表,因此 Json.Net 将其序列化为一个字符串数组。

这是一个使用动态对象的简单函数,但您也可以为您的composeMessage对象创建一个类:

[FunctionName("Function1")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
    ILogger log)
{
    var composeMessageList = new List<object>();
    for(var i = 0; i < 5; i++)
    {
        var composeMessage = new
        {
            __metadata = new
            {
                id = "",
                uri = "",
                type = ""
            },
            dateForSystem = "2019-05-17",
            timeForSystem = "13:15:51"
        };

        composeMessageList.Add(composeMessage);
    }

    var jsonToReturn = JsonConvert.SerializeObject(composeMessageList);
    return new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
    };
}

推荐阅读