将Json转换为Poco Collection /如何为For Each编码?

时间:2017-08-16 17:45:01

标签: azure azure-functions azure-logic-apps

我是Azure功能的新手。

我已经创建了一个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

所以我想为每个&#34;添加一个新的步骤。步。这是我现在得到的:(图片如下)。我所看到的只是&#34;身体&#34;从最初的触发器和&#34;转换&#34; webhook功能(我上面有)........

enter image description here

我如何获得&#34;人员#34; (所以我可以为每个人做一个)集合出现并可用于Logic App的下一步?

EDIT / APPEND:

最终游戏是为每个&#34;推送服务总线消息。我的人。

enter image description here

根据要求,这是&#34;人json&#34; .....

[{
    "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"
}]

2 个答案:

答案 0 :(得分:3)

确定。

在我忘记之前,我必须写下来。哇,真是个好车。

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

bind-address = 127.0.0.1

但是在“return returnReq;”上设置一个断点,我能够看到personWrapperJsonString包含下面的Json:

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);
        }
    }
}

确定。

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

enter image description here

然后我设置了Parse-Json。下方。

enter image description here

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

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

enter image description here

上面的内容当然会创建你需要的json-schema(适用于每个友好的)。如下所示。

enter image description here

现在我们离这么近了。

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

enter image description here

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

enter image description here

对于grins,我将sessionId设为PersonUuid值(只是为了表明我可以抓住对象的一个​​标量属性。(图片如下)。

enter image description here

现在json作为Service Bus消息的内容。 (下图)

enter image description here

然后我发布了Azure-Functions并部署了Logic-App,并向触发器发送了一个请求。

回到天蓝色的门户网站。 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。

答案 1 :(得分:2)

正如Steven Van Eycken所说,我们可以在逻辑应用程序中使用json fucntion解析字符串到数组。在您的情况下,我们可以直接从Azure函数解析Logic应用程序返回jarry中的字符串数组。我们可以选择以下方法之一来做到这一点。我也在我这边测试,它工作正常。

在逻辑App中

json(body('Your action name'))

enter image description here

enter image description here

直接在Azure功能中返回Jarry

var jarry =JArray.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(persons));

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

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