2017-08-16 76 views
1

我对Azure函数相当陌生。将Json转换为Poco Collection /如何为每个人编码?

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

所以我想添加一个新的台阶这是一个“为每个”步骤。这是我现在得到的:(下图)。我看到的是“身体”从最初的触发和“转换”功能的网络挂接(即我在上面)........

enter image description here

我如何得到“人”(所以我可以做一个for-each-person)集合以显示并可用于逻辑应用程序的下一步?

EDIT/APPEND:

结束游戏是推服务总线消息为我的人(多个)的“每个”。

enter image description here

按照要求,这里是“人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" 
}] 
+2

您可以添加由GenericWebHookCsharpOne函数返回的正文样本吗?看看你的代码,这将包含一系列“人员”。因此添加你的函数体应该足够了。你也可以在你的foreach之前添加一个“Parse JSON”动作。在此操作中,您可以定义(或使用示例生成)json模式。然后,您可以在for-each表达式中使用“Parse JSON”操作的输出。 –

+0

“人物”--Json补充道。我还会看看Parse-JSON操作。谢谢。 – granadaCoder

回答

3

好的。

在我忘记之前,我得把这个写下来。哇,真是太棒了。

首先是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); 
     } 
    } 
} 

但穿上断点 “返回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" 
    }] 
} 

确定。

然后我增加了一个 “解析JSON” 操作(下图)

enter image description here

然后我设置解析-JSON。下面。

enter image description here

上述解析-JSON设置未完成。

点击按钮“Use sample payload to generate schema”,这将弹出一个新窗口。从前面粘贴到你的“personWrapper”json中。如下图所示。

enter image description here

当然,上述将创建JSON的模式,你需要(即换各友好)。如下所示。

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函数并部署了逻辑应用程序,并向触发器发送了一个请求。

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

enter image description here

而且在服务总线资源管理器中进行快速检查,以 “窥视” 的消息的内容(如下图所示)

enter image description here

好,几个面包屑:

我从这里得到了一个关于将收集面设置为“包装”的提示。

Json.NET validate JSON array against Schema

一些错误,我一路上

了“无效的类型。预期目标,但得到数组。”

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

“这个输出是一个数组”,“一个foreach不能嵌套在另一个的foreach”

的“内的Json”预计其参数是一个字符串或XML.The提供的值的类型为“数组。

2

正如史蒂芬凡Eycken提到的,我们可以在逻辑应用解析字符串数组JSON温控功能。在你的情况下,我们可以直接从Azure函数中解析字符串到逻辑应用程序返回数据库。我们可以选择以下方式之一来实现这一点。我也在我身边测试它,它工作正常。

在逻辑应用

json(body('Your action name')) 

enter image description here

enter image description here

或者

返回雅里直接在天青功能

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); 
+0

因此,我研究了第一个版本,并准确地找到了你的位置。非常感谢那一个。我的最终游戏是将“每个”发送到服务总线队列。 (我想我应该在原文中提到)。在第一个版本中,我没有看到任何可以在服务总线队列设置中锁定的东西。我开始研究第二个版本,看看是否会产生不同的结果。 – granadaCoder

+0

你的回答绝对让我失望。我在这里写下了另一个答案,并写下了全文。谢谢你的帮助。 – granadaCoder