Thanks for the quick reply. I have it installed but not quite sure how to use it. I have a EmployeesController.cs file that generates my XML page. Do I have to create another cs file for just the JSON?
I complete missed the part about the api. Is this an asp.net mvc action or webapi? if it's webapi you just need to set the content-type and accept headers when you make a connection. Just set it to application/json and you're good.
Yes it's an MVC project, but that is an "ApiController" not a "Controller" therefore it's a WebAPI controller and not an asp.net mvc controller. By default ApiController knows how to serialize to json and xml.
As mentioned earlier, the code that calls this api endpoint needs to set the Accept and Content-Type headers.
Accept: application/json Content-Type: application/json
How are you calling this method? If it's via jquery, it's really easy to set the headers in jQuery's ajax function.
maddtechwf
Member
17 Points
147 Posts
Help with JSON output
Jan 30, 2013 08:53 PM|LINK
I have a pretty simple API that is displaying XML content. I would like to also make it show the content in JSON. Can anyone help me with this?
som-poddar
Member
30 Points
12 Posts
Re: Help with JSON output
Jan 30, 2013 09:16 PM|LINK
Please use Json.Net. I have been using it for a while. Use the following Links.
Wiki->http://james.newtonking.com/projects/json/help/
CodePlex project site http://json.codeplex.com/
Som Poddar
www.sombit.com/som
maddtechwf
Member
17 Points
147 Posts
Re: Help with JSON output
Jan 30, 2013 09:53 PM|LINK
Thanks for the quick reply. I have it installed but not quite sure how to use it. I have a EmployeesController.cs file that generates my XML page. Do I have to create another cs file for just the JSON?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Help with JSON output
Jan 30, 2013 10:17 PM|LINK
You don't need to create another class. If you have the data in an object just return Json in your controller like so:
public ActionResult GetData(){ var data = //fetch or create your object; return Json(data); }That will serialize the data to json for you.
Blog | Twitter : @Hattan
som-poddar
Member
30 Points
12 Posts
Re: Help with JSON output
Jan 30, 2013 11:44 PM|LINK
send us a snippet for your EmployeesController, I can help you out with a sample code.
Som Poddar
www.sombit.com/som
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Help with JSON output
Jan 31, 2013 03:04 AM|LINK
I complete missed the part about the api. Is this an asp.net mvc action or webapi? if it's webapi you just need to set the content-type and accept headers when you make a connection. Just set it to application/json and you're good.
Blog | Twitter : @Hattan
maddtechwf
Member
17 Points
147 Posts
Re: Help with JSON output
Jan 31, 2013 01:35 PM|LINK
This is an MVC project.
maddtechwf
Member
17 Points
147 Posts
Re: Help with JSON output
Jan 31, 2013 02:40 PM|LINK
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ServiceModel; using System.ServiceModel.Web; using System.Web.Http; namespace API.Controllers { public class EmployeesController : ApiController { private Data.DataClasses1DataContext _context = new Data.DataClasses1DataContext(); public List<Models.Employees> Get() { var employees = from e in _context.tbl_peoples where e.active == 1 select new Models.Employees { id = e.ID, Title = e.Name_Title, FirstName = e.Name_First, MiddleName = e.Name_Middle, LastName = e.Name_Last, email = e.email, //Websites = GetWebsites(e.ID), Websites = (from w_e in _context.tbl_websites where w_e.people_ID == e.ID select new Models.Website { WebsiteID = w_e.id, name = w_e.title, url = w_e.URL }).ToList(), //Positions = GetPositions(e.ID), Positions = (from position in _context.tbl_positions where position.people_ID == e.ID select new Models.Position { PositionID = position.id //,Department = position.dept_ID == null ? string.Empty : GetDepartment(position.dept_ID) ,Department = (from departments in _context.tbl_departments where departments.ID == position.dept_ID select departments.dept).FirstOrDefault() ,JobTitle = position.title //,Building = position.location_ID == null ? string.Empty : GetBuilding(position.location_ID) ,Building = (from buildings in _context.tbl_locations where buildings.id == position.location_ID select buildings.Name).FirstOrDefault() ,Room = position.room ,Phone = position.public3 == null ? string.Empty : "000-111-" + position.public3 ,Fax = position.fax3 == null ? string.Empty : "000-111-" + position.fax3 //,College = position.college_ID == null ? string.Empty ? GetCollege(position.college_ID) ,College = (from colleges in _context.tbl_colleges where colleges.id == position.college_ID select colleges.college).FirstOrDefault() }).ToList(), photo = "https://test.mysite.com/profiles/uploads/photos/" + e.PhotoFileName }; return employees.ToList(); } } }jp.lima
Member
186 Points
51 Posts
Re: Help with JSON output
Jan 31, 2013 03:36 PM|LINK
public JsonResult Get() { var data = //Your data return Json(data, JsonRequestBehavior.AllowGet); }CodeHobo
All-Star
18647 Points
2647 Posts
Re: Help with JSON output
Jan 31, 2013 04:28 PM|LINK
Yes it's an MVC project, but that is an "ApiController" not a "Controller" therefore it's a WebAPI controller and not an asp.net mvc controller. By default ApiController knows how to serialize to json and xml.
As mentioned earlier, the code that calls this api endpoint needs to set the Accept and Content-Type headers.
Accept: application/json
Content-Type: application/json
How are you calling this method? If it's via jquery, it's really easy to set the headers in jQuery's ajax function.
Blog | Twitter : @Hattan