How to pass multiple object as single json format
my object like below
public class Emp
{
public string Id{ get; set; }
public string Name{ get; set; }
}
Now i need to pass json like below
[
{
"Id": "John",
"Name": "US"
},
{
"Id": "John",
"Name": "US"
},
{
"Id": "John",
"Name": "US"
},
{
"Id": "John",
"Name": "US"
}
]
What I have tried:
I have tried with the below code but getting proper data for single json object.
But i need to get multiple data within square brackets.
Could you please suggest how to proceed to get multiple object with comma separated.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace ToCommaExample
{
class Program
{
static void Main(string[] args)
{
Service objService = new Service();
Service objService2 = new Service();
var listData = new List<SecurityAuditData>
{
new SecurityAuditData {TransactionName = "UserAdded",
EntityID = "useradded@test.com",
UserIdentity = "identity1"},
};
var listData2 = new List<SecurityAuditData>
{
new SecurityAuditData {TransactionName = "SecurityRoleUpdated",
EntityID = "SecurityRoleUpdated@test.com",
UserIdentity = "identity2"},
};
objService.AuditData = listData;
objService2.AuditData = listData2;
var finalJson = JsonConvert.SerializeObject(objService2.AuditData, Formatting.Indented);
Console.WriteLine(finalJson);
Console.Read();
}
}
public class SecurityAuditData
{
public string TransactionName { get; set; }
public string EntityID { get; set; }
public string UserIdentity { get; set; }
}
class Service
{
[NonSerialized]
public object AuditData;
}
}
Present output like below
[
{
"TransactionName": "SecurityRoleUpdated",
"EntityID": "SecurityRoleUpdated@test.com",
"UserIdentity": "identity2"
}
]
Expected output
[
{
"TransactionName": "SecurityRoleUpdated",
"EntityID": "SecurityRoleUpdated@test.com",
"UserIdentity": "identity2"
},
{
"TransactionName": "UserAdded",
"EntityID": "useradded@test.com",
"UserIdentity": "identity1"
}
]