C# 如何组合对象,以便将所有对象同时显示为JSON?

C# 如何组合对象,以便将所有对象同时显示为JSON?,c#,C#,在下面的代码中,选中以下行: //here I need to put the object "nd" into a "bucket" so that I can finish the loop and then return EVERYTHING together. 我的问题是,如何组合对象以作为JSON返回?我之所以需要“合并”,是因为循环将值分配给这个类的特定属性。一旦每个类都完成了属性值的获取,我需要将所有内容作为JSON返回 namespace X { public clas

在下面的代码中,选中以下行:

//here I need to put the object "nd" into a "bucket" so that I can finish the loop and then return EVERYTHING together.
我的问题是,如何组合对象以作为JSON返回?我之所以需要“合并”,是因为循环将值分配给这个类的特定属性。一旦每个类都完成了属性值的获取,我需要将所有内容作为JSON返回

namespace X
{
    public class NotificationsController : ApiController
    {
        public List<NotificationTreeNode> getNotifications(int id)
        {
            var bo = new HomeBO();
            var list = bo.GetNotificationsForUser(id);
            var notificationTreeNodes = (from GBLNotifications n in list
                                        where n.NotificationCount != 0
                                        select new NotificationTreeNode(n)).ToList();

            foreach (var notificationTreeNode in notificationTreeNodes)
            {
                Node nd = new Node();
                nd.notificationType = notificationTreeNode.NotificationNode.NotificationType;

                var notificationList = bo.GetNotificationsForUser(id, notificationTreeNode.NotificationNode.NotificationTypeId).Cast<GBLNotifications>().ToList();
                List<string> notificationDescriptions = new List<string>();

                foreach (var item in notificationList)
                {
                    notificationDescriptions.Add(item.NotificationDescription);
                }

                nd.notifications = notificationDescriptions;

                //here I need to put the object "nd" into a "bucket" so that I can finish the loop and then return EVERYTHING together.
            }

            return bucket;
        }
    }

    public class Node
    {
        public string notificationType
        {
            get;
            set;
        }

        public List<string> notifications
        {
            get;
            set;
        }
    }
}
名称空间X
{
公共类通知控制器:ApiController
{
公共列表getNotifications(int-id)
{
var bo=新的HomeBO();
var list=bo.getnotificationsfourser(id);
var notificationTreeNodes=(来自列表中的GBLNotifications n
其中n.NotificationCount!=0
选择new NotificationTreeNode(n)).ToList();
foreach(notificationTreeNodes中的var notificationTreeNode)
{
Node nd=新节点();
nd.notificationType=notificationTreeNode.NotificationNode.notificationType;
var notificationList=bo.getnotificationsfourser(id,notificationTreeNode.NotificationNode.NotificationTypeId.Cast().ToList();
List notificationDescriptions=新列表();
foreach(通知列表中的var项)
{
NotificationDescription.Add(item.NotificationDescription);
}
通知=通知说明;
//在这里,我需要将对象“nd”放入一个“bucket”中,以便完成循环,然后一起返回所有内容。
}
返回铲斗;
}
}
公共类节点
{
公共字符串notificationType
{
得到;
设置
}
公开名单通知
{
得到;
设置
}
}
}

在迭代源集合时,只需将每个项添加到列表中即可:

public List<Node> getNotifications(int id)
{
    var bucket = new List<Node>(notificationTreeNodes.Count);

    foreach (var notificationTreeNode in notificationTreeNodes)
    {
        Node nd = new Node();
        ...

        bucket.Add(nd);
    }

    return bucket;
}
public List getNotifications(int-id)
{
var bucket=新列表(notificationTreeNodes.Count);
foreach(notificationTreeNodes中的var notificationTreeNode)
{
Node nd=新节点();
...
桶。添加(nd);
}
返回铲斗;
}

在迭代源集合时,只需将每个项添加到列表中即可:

public List<Node> getNotifications(int id)
{
    var bucket = new List<Node>(notificationTreeNodes.Count);

    foreach (var notificationTreeNode in notificationTreeNodes)
    {
        Node nd = new Node();
        ...

        bucket.Add(nd);
    }

    return bucket;
}
public List getNotifications(int-id)
{
var bucket=新列表(notificationTreeNodes.Count);
foreach(notificationTreeNodes中的var notificationTreeNode)
{
Node nd=新节点();
...
桶。添加(nd);
}
返回铲斗;
}