The UI is sending the payload in the json format : [{"ContractInvoicePerDays":[{"33262":"2023-03-24T13:32:47.313"},{"33263":"2023-03-24T13:32:56.967"}]}]
it is comma seperated string
my c# code is :
C#
List TimeCardIDs = cipdRejectedView.ContractInvoicePerDays.Split(',').Select(int.Parse).ToList();
if (TimeCardIDs.Count == 1)
{
BaseStateProvinceID = cipdRejectedView.BaseStateProvinceID;
}
foreach (var item in TimeCardIDs)
{
ContractInvoicePerDay contractInvoicePerDay = new ContractInvoicePerDay();
int _ContractInvoicePerDayID = item;
contractInvoicePerDay = db.ContractInvoicePerDays.Find(_ContractInvoicePerDayID);
Ali BenchaabanRank #925 · 2.2K reputationPosted Mar 27, 2023, 8:58 PM EDT
Hello,
You can iterate over a JSON object using dynamic object
public int IterateOverJsonDynamically() { var jsonData = JsonConvert.DeserializeObject(ContractInvoicePerDays); foreach (var data in jsonData) { var id = data.id; var date = data.date; Console.WriteLine($"Id: {id}, date: {date}); } var count = jsonData.Count; return count; }Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 27, 2023, 11:13 PM EDT
Hello Basit,
You can use a JSON library such as Newtonsoft.Json to deserialize the JSON payload into a C# object, and then use LINQ to loop through the key-value pairs inside the "ContractInvoicePerDays" array.
Here's an example:
using Newtonsoft.Json.Linq; // Add this namespace // Deserialize the JSON payload into a JObject var payload = JArray.Parse(cipdRejectedView.ContractInvoicePerDays)[0]; // Loop through the key-value pairs inside the "ContractInvoicePerDays" array using LINQ var contractInvoicePerDays = payload["ContractInvoicePerDays"] .SelectMany(kvp => kvp.Properties()) .Select(kvp => new ContractInvoicePerDay { ContractInvoicePerDayID = int.Parse(kvp.Name), InvoiceDate = DateTime.Parse(kvp.Value.ToString()) }); foreach (var contractInvoicePerDay in contractInvoicePerDays) { // Do something with the ContractInvoicePerDay object // ... }Thanks
Naimish Makwana