“我在这个函数中创建了一个Azure函数,我调用一个返回JSON的API。我想将此JSON解析为一个对象,以便我可以在函数中使用它。我不能不使用Newton.JSON,因为该函数看起来不知道这一点。 如何解析 JSON?”
最佳答案
以下是使用 JsonNet 序列化/反序列化对象的完整 Azure Function 源代码:
#r "Newtonsoft.Json"
using System.Net;
using Newtonsoft.Json;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
dynamic body = await req.Content.ReadAsStringAsync();
var e = JsonConvert.DeserializeObject<EventData>(body as string);
return req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(e));
}
public class EventData
{
public string Category { get; set; }
public string Action { get; set; }
public string Label { get; set; }
}
示例输入(请求正文):
{
"Category": "Azure Functions",
"Action": "Run",
"Label": "Test"
}
示例输出:
"{\"Category\":\"Azure Functions\",\"Action\":\"Run\",\"Label\":\"Test\"}"
关于json - 如何在 Azure 函数中解析 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094170/