Thursday, January 24, 2019

Handling return data

I have a POST that is returning an array of objects that looks like this -

[ { "ReferenceId":"3817d1b2-5423-4c36-85d7-9edd7148", "SystemMessage":"Success", "UserMessage":"Success" }, [ { "id":"6e91d-9d70-44db-8b06-c4d27854", "name":"MVP_Gold_Outpatient_Approval_ContinuedStay - 2019/01/03 01:51 PM", "author":"api_Dev", "startDate":"2019-01-03T19:51:48.35Z", "endDate":"2019-01-03T19:51:48.35Z", "duration":"PT0S", "status":"WaitingForInput", "workflow":"dev//dev//dev//", "workspace":"Dev DEv", "communication":"dev_dev_Dev_dev", "type":"Interactive", "manualOutcome":null, "progress":0, "currentStep":null, "userId":"t@t.com" }, { "id":"6e91d-9d70-44db-8b06-c4d27854", "name":"MVP_Gold_Outpatient_Approval_ContinuedStay - 2019/01/03 01:51 PM", "author":"api_Dev", "startDate":"2019-01-03T19:51:48.35Z", "endDate":"2019-01-03T19:51:48.35Z", "duration":"PT0S", "status":"WaitingForInput", "workflow":"dev//dev//dev//", "workspace":"Dev DEv", "communication":"dev_dev_Dev_dev", "type":"Interactive", "manualOutcome":null, "progress":0, "currentStep":null, "userId":"t@t.com" } ] ] 

And I want to loop through the second array and add it to a list. My controller

public async Task<List<CorrespondenceJob>> GetCurrentJobsForAuthDetailId(string request, string authDetailId) { using (var client = new HttpClient()) { DateTime startDate = DateTime.Now; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(ConfigurationManager.AppSettings["CMHeaderScheme"], ConfigurationManager.AppSettings["CMAPIKey"]); List<CorrespondenceJob> jobList = new List<CorrespondenceJob>(); using (HttpResponseMessage response = await client.PostAsync($"{correspondenceManagerUtility.JobRenderCorrespondence}&authDetailId={authDetailId}", new StringContent(request, Encoding.UTF8, "application/json"))) { if (response.IsSuccessStatusCode) { string jsonString = response.Content.ReadAsStringAsync().Result; if(JArray.Parse(jsonString)[0]["ReferenceId"] != null) { var jobs = JArray.Parse(jsonString); foreach (var x in jobs.Skip(1)) { jobList = JArray.Parse(jsonString)[x].ToObject<List<CorrespondenceJob>>(); //jobList.Id= x.Id; } } } } DateTime endDate = DateTime.Now; LogHelper.LogServiceTimings(startDate, endDate, "dev", "dev"); return jobList; } } 

I'm skipping the first [0] item in the array because that is something returned from the gateway, but first checking that this gateway reference is returned. Then I want to loop through and add the returned data to the jobList. My controller above is not working and just returning out on the JArray.Parse()[x].ToObject. What is the best way to accomplish returning this json into a complete list?

Handling return data Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team