2017-08-16 36 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