How to create a json structure from the data list?

advertisements

I want to create json structure with nested object from my list object.

This is my class:

public class Employee
{
     public int EmployeeId { get; set; }
     public int Skillssetpoints { get; set; }
     public string Name { get; set; }
     public Nullable<System.DateTime> Date { get; set; }
}

public class EmployeeModel
{
     public int EmployeeId { get; set; }
     public List<int> Skillssetpoints { get; set; }
     public string Name { get; set; }
     public Nullable<System.DateTime> Date { get; set; }
}

Records are like this:

EmployeeId   SkillssetPoints   Date
 1              10             4/5/2016 16:12:12
 2              12             3/5/2016 17:12:12
 3              4              8/5/2016 8:12:12
 4              20             1/5/2016 2:12:12

This is how i am getting data:

var data=context.Employee.Tolist();

After getting data i want to create this json structure from above data using EmployeeModel and return:

Expected Output:

 {"Date":"8-5-2016 08:12:12","SkillssetPoints":[4,10,12,20]}

In Date field i would take Highest date so 8-5-2016 and SkillssetPoints will be order by ascending.

How to create this json structure with my EmployeeModel class??


Add a reference to the nuget package Newtonsoft.Json then use ...

string result = JsonConvert.Serialize(data);

It looks like you need to translate the data in your db to the model format by doing a projection first then serialize the result ...

var groupedData = data
    .GroupBy(s => s.EmployeeId)
    .OrderBy(s => s.Date)
    .Select(g => new EmployeeModel {
        EmployeeId = g.Key,
        Name = g.First().Name,
        Date = g.First().Date,
        Skillssetpoints = g.Select(s => s.Skillssetpoints).OrderBy(i => i).ToList()
    });

That should produce a collection of this model ...

public class EmployeeModel
{
    public int EmployeeId { get; set; }
    public List<int> Skillssetpoints { get; set; }
    public string Name { get; set; }
    public DateTime? Date { get; set; }
}

... when I do this ...

var data = new List<EmployeeModel> {
    new EmployeeModel { EmployeeId = 1, Name = "Homer Simpson", Skillssetpoints = new List<int> { 1,2,3,4 }, Date = DateTime.Now },
    new EmployeeModel { EmployeeId = 2, Name = "Marge Simpson", Skillssetpoints = new List<int> { 1,2,3,4 }, Date = DateTime.Now },
    new EmployeeModel { EmployeeId = 3, Name = "Lisa Simpson", Skillssetpoints = new List<int> { 1,2,3,4 }, Date = DateTime.Now },
    new EmployeeModel { EmployeeId = 4, Name = "Bart Simpson", Skillssetpoints = new List<int> { 1,2,3,4 }, Date = DateTime.Now }
};

var result = JsonConvert.SerializeObject(data);

I get this output ...

[
    {
        "EmployeeId": 1,
        "Skillssetpoints": [1,2,3,4],
        "Name": "Homer Simpson",
        "Date": "2016-04-05T11:42:09.9126748+01:00"
    },
    {
        "EmployeeId": 2,
        "Skillssetpoints": [1,2,3,4],
        "Name": "Marge Simpson",
        "Date": "2016-04-05T11:42:09.9126748+01:00"
    },
    {
        "EmployeeId": 3,
        "Skillssetpoints": [1,2,3,4],
        "Name": "Lisa Simpson",
        "Date": "2016-04-05T11:42:09.9126748+01:00"
    },
    {
        "EmployeeId": 4,
        "Skillssetpoints": [1,2,3,4],
        "Name": "Bart Simpson",
        "Date": "2016-04-05T11:42:09.9126748+01:00"
    }
]