gpt4 book ai didi

azure - 将 Json 转换为 Poco Collection/如何编写 For Each?

转载 作者:行者123 更新时间:2023-12-01 23:07:26 25 4
gpt4 key购买 nike

我对 Azure Functions 还很陌生。

我创建了一个 C# WebHook/Azure 函数(我想这是正确的事情)来获取我的 json 内容并将其转换为简单的 poco/dto 对象的集合。

public static class GenericWebHookCSharp
{
[FunctionName("GenericWebHookCsharpOne")]
public static async Task<HttpResponseMessage /* object */> Run([HttpTrigger(WebHookType = "genericJson")]HttpRequestMessage req, TraceWriter log)
{
try
{

log.Info(string.Format("C# GenericWebHookCsharpOne about to process a request. ('{0}')", DateTime.Now.ToLongTimeString()));

//IUnityContainer container = new UnityContainer();
//container.RegisterType<IJsonToPersonRequestWrapperConverter, JsonToPersonRequestWrapperConverter>();
//IJsonToPersonRequestWrapperConverter jsonConvtr = container.Resolve<IJsonToPersonRequestWrapperConverter>();
//ICollection<Person> emps = await jsonConvtr.ConvertHttpRequestMessageToPersonCollection(req);

/* above commented code is my "real" code where I take the INPUT request-body-as-json and convert it into a ICollection of Person(s) */
/* below code, I just fake-creating some persons */
string jsonContent = await req.Content.ReadAsStringAsync();
ICollection<Person> persons = new List<Person>();
for(int i = 0; i< 10; i++)
{
persons.Add(new Person() { PersonUuid = Guid.NewGuid(), LastName = "LN" + i.ToString(), FirstName = "FN" + i.ToString(), BirthDate = DateTimeOffset.Now.AddYears(-1 * (20 + i))});
}

string serializedJson = Newtonsoft.Json.JsonConvert.SerializeObject(persons);

log.Info(string.Format("C# GenericWebHookCsharpOne finished a request. ('{0}')", DateTime.Now.ToLongTimeString()));

return req.CreateResponse(HttpStatusCode.OK , serializedJson);

}
catch (Exception ex)
{
string errorMsg = ex.Message;// ExceptionHelper.GenerateFullFlatMessage(ex);
log.Error(errorMsg);
return req.CreateResponse(HttpStatusCode.BadRequest, errorMsg);
}
}
}

在另一个 .cs 文件中

public class Person
{

public Guid PersonUuid { get; set; }

public string LastName { get; set; }

public string FirstName { get; set; }

public DateTimeOffset? BirthDate { get; set; }
}

如果我在 Visual Studio 中调试它,它工作正常。

因此,我将其添加为我的逻辑应用程序中的一个步骤,如下所示

enter image description here

所以我想添加一个新步骤,即“针对每个”步骤。这是我现在得到的:(下图)。我看到的只是初始触发器中的“body”和“convert”webhook 函数(我上面有)........

enter image description here

如何让“人员”(以便我可以为每个人执行)集合显示并可用于逻辑应用上的下一步?

编辑/追加:

最终的游戏是为我的“每个人”推送一条服务总线消息。

enter image description here

根据要求,这里是“person json”......

[{
"PersonUuid": "7ec8cc4d-831c-4c89-8516-47424ee2658d",
"LastName": "LN0",
"FirstName": "FN0",
"BirthDate": "1997-08-17T09:46:16.9839382-04:00"
},
{
"PersonUuid": "275264bc-5a86-476d-a189-512afa1e3ce4",
"LastName": "LN1",
"FirstName": "FN1",
"BirthDate": "1996-08-17T09:46:16.9844385-04:00"
},
{
"PersonUuid": "e522b827-2d2e-465d-a30a-c4b619d2e8e4",
"LastName": "LN2",
"FirstName": "FN2",
"BirthDate": "1995-08-17T09:46:16.9844385-04:00"
},
{
"PersonUuid": "f16bce36-3491-4519-bc82-580939f61b2e",
"LastName": "LN3",
"FirstName": "FN3",
"BirthDate": "1994-08-17T09:46:16.9844385-04:00"
},
{
"PersonUuid": "42456057-39ef-45aa-bd7c-ad6a8fa74fd1",
"LastName": "LN4",
"FirstName": "FN4",
"BirthDate": "1993-08-17T09:46:16.9844385-04:00"
},
{
"PersonUuid": "14088a6e-3c44-4cb0-927d-19f5eda279c4",
"LastName": "LN5",
"FirstName": "FN5",
"BirthDate": "1992-08-17T09:46:16.9844385-04:00"
},
{
"PersonUuid": "332a5cde-3cd1-467a-9dfc-2b187d6ae32e",
"LastName": "LN6",
"FirstName": "FN6",
"BirthDate": "1991-08-17T09:46:16.9844385-04:00"
},
{
"PersonUuid": "6debe134-19e6-4b16-a91d-05ded511eff6",
"LastName": "LN7",
"FirstName": "FN7",
"BirthDate": "1990-08-17T09:46:16.9844385-04:00"
},
{
"PersonUuid": "e61ef8a1-09d3-4c5b-b948-df8e0858cd29",
"LastName": "LN8",
"FirstName": "FN8",
"BirthDate": "1989-08-17T09:46:16.9844385-04:00"
},
{
"PersonUuid": "e9b27632-d3a4-4fe8-8745-04edfa8854f7",
"LastName": "LN9",
"FirstName": "FN9",
"BirthDate": "1988-08-17T09:46:16.9844385-04:00"
}]

最佳答案

好的。

我必须在忘记之前把它写下来。哇,真是一次美妙的旅程。

首先是 WebHook 上的 C# 代码。请注意“anonymousPersonWrapper”代码。

public static class GenericWebHookCSharp
{
[FunctionName("GenericWebHookCsharpOne")]
public static async Task<HttpResponseMessage /* object */> Run([HttpTrigger(WebHookType = "genericJson")]HttpRequestMessage req, TraceWriter log)
{
try
{

log.Info(string.Format("C# GenericWebHookCsharpOne about to process a request. ('{0}')", DateTime.Now.ToLongTimeString()));

///////* below code, I just fake-creating some persons */
string jsonContent = await req.Content.ReadAsStringAsync();
ICollection<Person> persons = new List<Person>();
for (int i = 0; i < 3; i++)
{
persons.Add(new Person() { PersonUuid = Guid.NewGuid(), LastName = "LN" + i.ToString(), FirstName = "FN" + i.ToString(), BirthDate = DateTimeOffset.Now.AddYears(-1 * (20 + i)) });
}

/* the below is the "trick" to allow the for-each to work in the Logic-App. at least in my experience */
var anonymousPersonWrapper = new
{
personWrapper = persons
};

string personWrapperJsonString = JsonConvert.SerializeObject(anonymousPersonWrapper);

log.Info(string.Format("C# GenericWebHookCsharpOne finished a request. ('{0}')", DateTime.Now.ToLongTimeString()));
HttpResponseMessage returnReq = req.CreateResponse(HttpStatusCode.OK , personWrapperJsonString );
return returnReq;

}
catch (Exception ex)
{
string errorMsg = ex.Message;
log.Error(errorMsg);
return req.CreateResponse(HttpStatusCode.BadRequest, errorMsg);
}
}
}

但是在“return returnReq;”上放置一个断点,我可以看到 personWrapperJsonString 包含以下 Json:

{
"personWrapper": [{
"PersonUuid": "31fb318d-a9bf-4c2f-ad16-0810ddd73746",
"LastName": "LN0",
"FirstName": "FN0",
"BirthDate": "1997-08-17T15:10:08.9633612-04:00"
},
{
"PersonUuid": "73fdacc7-e1e8-48ff-b161-1bd8b5f4aec1",
"LastName": "LN1",
"FirstName": "FN1",
"BirthDate": "1996-08-17T15:10:08.9633612-04:00"
},
{
"PersonUuid": "d18b4324-2d3e-41ca-9525-fe769af89e9c",
"LastName": "LN2",
"FirstName": "FN2",
"BirthDate": "1995-08-17T15:10:08.9633612-04:00"
}]
}

好的。

然后我添加了一个“Parse Json”操作(下图)

enter image description here

然后我设置 Parse-Json。下面。

enter image description here

上面的 parse-json 设置尚未完成。

单击“使用示例有效负载生成架构”按钮,将弹出一个新窗口。粘贴之前的“personWrapper”json。如下图所示。

enter image description here

上面的内容当然会创建您需要的 json 模式(即 for-each 友好)。如下所示。

enter image description here

现在我们已经很接近了。

添加 For-Each(添加新步骤时使用“更多”按钮)(如下所示)

enter image description here

现在我们设置 for-each。看看出现了什么! “personWrapper”(下图)

enter image description here

为了笑,我将 sessionId 设置为 PersonUuid 值(只是为了表明我可以获取对象的标量属性之一。(下图)。

enter image description here

现在 json 作为服务总线消息的内容。 (下图)

enter image description here

然后,我发布了 Azure-Functions 并部署了逻辑应用程序,并向触发器发送了请求。

返回 azure 门户。 PersonUuid 显示为 SessionId! (下图)

enter image description here

并在 Service Bus Explorer 中快速检查以“查看”消息的内容(下图)

enter image description here

好的,一些面包屑:

我从这里得到了关于将集合侧作为“包装器”的提示。

Json.NET validate JSON array against Schema

我一路上遇到的一些错误

“类型无效。需要对象,但得到的是数组。”

UnsupportedMediaType“WebHook 请求必须包含格式为 JSON 的实体主体。”

“此输出是一个数组”“一个 foreach 不能嵌套在另一个 foreach 中”

“Json”期望其参数是字符串或 XML。提供的值是“Array”类型。

关于azure - 将 Json 转换为 Poco Collection/如何编写 For Each?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45719964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com