c# - 如何从 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 函数实现这种格式输出?
或者如何将其格式化为逻辑应用程序?
解决方案
问题是序列化对象是一个字符串列表,因此 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")
};
}
推荐阅读
- javascript - Redux useSelect 未按预期更新
- node.js - Mac 10.13 VSC 中的 Node.js 标头弃用错误消息
- javascript - 如何为我的控制台 Nodejs Web 应用程序创建 UI?
- php - Wordpress WP 管理员问题
- android - ClassNotFoundException 仅在使用 Android Studio 4.1.2 和 Android Application Bundle 从 Google Play 下载时
- mysql - 将单独的 Where 子句应用于单独的列 (SQL)
- python-3.x - 如何在 Codalab 中安装外部库
- ansible - 如何将 Ansible playbook 导出或转换为 Bash 脚本?
- android - React Native run-android 由于“无法删除陈旧的输出文件”错误而失败
- sql - 按行分组的 SQL 联合