1

All I want to do is return a JSON to the browser.

This is what gets returned right now -- it's the JSON but it's inside a string. How do I return just a JSON?

And here's my code:

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;
        }
    }
}

If I try to do a GET with the URL for this controller, Fiddler is not showing anything under JSON.

Anyone know what the issue is here?

1 Answer 1

4

Because you return JSON:

var oSerializer = new JavaScriptSerializer();
string sJSON = oSerializer.Serialize(lOfNodes);
return sJSON;

Instead of doing this you should just return lOfNodes (and change the return value to List<Node>) and rely on the built-in Content Negotiation.

Web API will return XML or JSON depending on the Accept header. If you need other formats, you could write you own formatter easily.

EDIT:

Since you are having some problems with Kendo UI (I don't know how the request are made) it might help to remove the XML formatter explicit. See this post for an example.

4
  • the problem is that I need to return a JSON so I can use it in my KendoUI TreeView...
    – JJ.
    Jul 29, 2013 at 18:56
  • Web API will handle this for you depending on the "Accept" header. Jul 29, 2013 at 18:59
  • I reverted it back to what I had which is exactly what you told me to do. but im having a hard time getting kendo ui to work... are you any good with it? im able to display the parent items but not the children :(
    – JJ.
    Jul 29, 2013 at 19:09
  • I am familiar with Web API, but not with Kendo UI. Jul 29, 2013 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.