我要做的就是向浏览器返回一个 JSON。
这就是现在返回的内容——它是 JSON,但它在字符串中。如何只返回 JSON?
这是我的代码:
namespace ...Controllers
{
public class NotificationsController : ApiController
{
public string 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();
List<Node> lOfNodes = new List<Node>();
foreach (var notificationTreeNode in notificationTreeNodes)
{
Node nd = new Node();
nd.notificationType = notificationTreeNode.NotificationNode.NotificationType + " " + "(" + notificationTreeNode.NotificationNode.NotificationCount + ")";
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;
lOfNodes.Add(nd);
}
var oSerializer = new JavaScriptSerializer();
string sJSON = oSerializer.Serialize(lOfNodes);
return sJSON;
}
}
public class Node
{
public string notificationType
{
get;
set;
}
public List<string> notifications
{
get;
set;
}
}
}
如果我尝试使用此控制器的 URL 进行 GET,Fiddler 不会在 JSON 下显示任何内容。
有人知道这里有什么问题吗?