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?

asked Jul 29, 2013 at 18:52

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.

answered Jul 29, 2013 at 18:54
4
  • the problem is that I need to return a JSON so I can use it in my KendoUI TreeView... Commented Jul 29, 2013 at 18:56
  • Web API will handle this for you depending on the "Accept" header. Commented 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 :( Commented Jul 29, 2013 at 19:09
  • I am familiar with Web API, but not with Kendo UI. Commented Jul 29, 2013 at 19:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.