JJ.
JJ.

Reputation: 9970

How do I construct a JSON from dynamic data?

I have two collections:

The first one is the "parent" node:

e.g.

Cars
People
Companies
...

Under each of these, there is some data.

  1. Cars
    1.1. Honda
    1.2. Ford
  2. People
    2.1. Harrison Ford
    2.2. Psy
    2.3. Jessica Alba
  3. Companies
    3.1. Oracle
    3.2. Microsoft

and so on...

These are items I can iterate over in my C# application.

How would I go about constructing a dataTable (or whatever object would work best with it) in order to producer a JSON that mimics the above tree?

This is what I have but it doesn't really give me what I need:

public DataTable getNotificationTypeByUser(int id)
        {
            var bo = new HomeBO();
            var list = bo.GetNotificationsForUser(id);
            var notificationTreeNode = (from GBLNotifications n in list
                                 where n.NotificationCount != 0
                                 select new NotificationTreeNode(n)).ToList();

            var table = new DataTable();

            var column1 = new DataColumn
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "NotificationType"
            };

            table.Columns.Add(column1);

            var column2 = new DataColumn
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "NotificationDescription"
            };

            table.Columns.Add(column2);

            foreach (var node in notificationTreeNode)
            {
                var row1 = table.NewRow();
                row1["NotificationType"] = node.ToString();
                table.Rows.Add(row1);

                var notifications = bo.GetNotificationsForUser(id, node.NotificationNode.NotificationTypeId);

                foreach (GBLNotifications n in notifications)
                {
                    var row = table.NewRow();
                    row["NotificationDescription"] = n.NotificationDescription;
                    table.Rows.Add(row);
                } 
            }
            return table;
        }

Upvotes: 0

Views: 236

Answers (3)

yavuz
yavuz

Reputation: 332

what about this one.

        var Cars = new[] { "Honda", "Ford"};
        var People = new[] { "Harrison Ford", "Psy", "Jessica Alba" };
        var Companies = new[] { "Oracle", "Microsoft" };
        var result =  new {Cars, People, Companies };
        string json = Newtonsoft.Json.JsonConvert.SerializeObject(result);

above code produces string below...

{ "Cars": [ "Honda", "Ford" ], "People": [ "Harrison Ford", "Psy", "Jessica Alba" ], "Companies": [ "Oracle", "Microsoft" ] }

Upvotes: 0

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15158

Well, not sure what's going on in your code, but judging by your hierarchy description you need objects like this:

// along with their properties
public class Car { }
public class Person { }
public class Company { }

public class DataAggregate
{
    public List<Car> Cars { get; set; }
    public List<Person> People { get; set; }
    public List<Company> Companies { get; set; }
}

Then you serialize them like:

public void SerializeData()
{
    var aggregate = new DataAggregate();
    // fill the aggregate accordingly -> from your data source (data tables or what have you)

    // this now has the JSON format format you're looking for
    string jsonData = JsonConvert.SerializeObject(aggregate);
}

I really hope I didn't misunderstand your question.

Upvotes: 1

Ehsan
Ehsan

Reputation: 32721

Assuming that you already have Collections you can use any of the serializers around like Newtonsoft to serialize that to JSON

like

using Newtonsoft.Json;

string json = JsonConvert.SerializeObject(yourlist);

Upvotes: 2

Related Questions