Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"
}
]
Posted
Updated 2-Oct-18 23:50pm
v2
Comments
F-ES Sitecore 1-Oct-18 6:40am    
Have a List<Emp> or an array of Emp and it will serialise to how you want.
DGKumar 3-Oct-18 5:53am    
Hi I have updated the query that what i have tried to get multiple objects json data
Could you please suggest how to get data.

yes its very simple you can pass this list to JSON Serialize

using Newtonsoft.Json;

var json = JsonConvert.SerializeObject(yourlist_here);

for this you have to install this into your project via the NuGet Package Manager (Tools --> NuGet Package Manager --> Package Manager Console):

PM> Install-Package Newtonsoft.Json
 
Share this answer
 
Comments
DGKumar 1-Oct-18 8:20am    
HI
By using this code i am bale to get only one result
But need to get multiple results
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication22
{
class Program
{







static void Main(string[] args)
{
JsonResultMethods obj = new JsonResultMethods();

JsonResult objJsonResult = new JsonResult();
var finalJson = JsonConvert.SerializeObject(obj.M1());
Console.WriteLine(finalJson);
Console.Read();
}

}

class JsonResult {
public int Id { get; set; }
public string Name { get; set; }

}
class JsonResultMethods
{

public List<jsonresult> M1()
{
List<jsonresult> objList = new List<jsonresult>() { new JsonResult() { Id = 1, Name = "Test" } };
return objList;
}
public List<jsonresult> M2()
{
List<jsonresult> objList = new List<jsonresult>() { new JsonResult() { Id = 19, Name = "Test2" } };
return objList;
}
}



}
Getting Result: [{"Id":1,"Name":"Test"}]
Actual result should be like [{"Id":1,"Name":"Test"},{"Id":19,"Name":"Test2"}]
DGKumar 3-Oct-18 5:28am    
I want json data like below for multiple objects with same class properties.
[{},{},{}]
Member 14153700 27-Apr-20 3:13am    
Hi bro.. i am stuck with this now. can you please forward me some code. it will be great help i am trying to implement in asp.net core web api
Do something like this

var finalJson = json.Serialize(classObj);
 
Share this answer
 
Comments
DGKumar 1-Oct-18 7:45am    
HI Summiya,

I have to add object individually like below and object should get from different functionality but fields are same.

Emp obj=new Emp();
obj.Id=123,
obj.Name="Test1"
addList(obj);

Emp obj1=new Emp();
obj1.Id=1234,
obj1.Name="Test12"
addList(obj1);
Emp obj2=new Emp();
obj2.Id=12335,
obj2.Name="Test13"
addList(obj2);
Emp obj3=new Emp();
obj3.Id=1233,
obj3.Name="Test14"
addList(obj3);

finally i need to pass all these object with single json object to API.
API should get this type of result.
[
{
"Id": "John",
"Name": "US"
},
{
"Id": "John",
"Name": "US"
},
{
"Id": "John",
"Name": "US"
},
{
"Id": "John",
"Name": "US"
}
]

The below link is getting from jquery level but i want to implement at business logic level
http://www.binaryintellect.net/articles/bfbda4a5-17ba-4820-824a-4a25373379a1.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900