haji

haji

  • NA
  • 7
  • 17.9k

How to get JSON response when the returning object is list ?

Sep 8 2012 1:42 AM

I have written a WCF REST service.My Get method would return a list values..When I execute my xml response is :

<GetAppointmentList xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<AppointmentList>
<Company>
<CompanyId>bf98bd3b-dccc-442d-ab4b-0bad4ecbdfd7</CompanyId>
<CompanyName>Care</CompanyName>
<AppointmentStartTime>9</AppointmentStartTime>
<AppointmentEndTime>19</AppointmentEndTime>
<BreakStartTime>12:00</BreakStartTime>
<BreakEndTime>13:00</BreakEndTime>
<Interval>10</Interval>
</Company>
</AppointmentList>

How ever when I tried the same with JSON response Iam getting like :
bf98bd3b-dccc-442d-ab4b-0bad4ecbdfd7Care919 12:0013:00 10
i.e JSON response is not clear..I want the response in 
 
"AppointmentList":[
{
"AppointmentEndTime":"19 ",
"AppointmentStartTime":"9",
"BreakEndTime":"13:00",
"BreakStartTime":"12:00",
"CompanyId":"bf98bd3b-dccc-442d-ab4b-0bad4ecbdfd7",
"CompanyName":"Care",
"Interval":"10"
}
]
 
How can I get a clear response like this in JSON format?
 IService.cs: 

[OperationContract]
       [WebInvoke(UriTemplate = "GetAppointments/?companyId={companyId}&appointmentDate={appointmentDate}", Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
     GetAppointmentList GetAppointment(Guid companyId, string appointmentDate);

Service1.cs :   

public GetAppointmentList GetAppointment(Guid companyId, string appointmentDate)
        {
      

            SQLDataContext context = new SQLDataContext();

            var getappointment = context.GetAppointmentTimings(companyId,appointmentDate);
           GetAppointmentList getappointmentlist = new GetAppointmentList();

            getappointmentlist.AppointmentList = new List<Company>().ToList();
         
            foreach (var r in getappointment.ToList())
            {
                Company company = new Company();
                company.CompanyId = r.CompanyId;
                company.CompanyName = r.CompanyName;
                company.BreakStartTime = r.BreakStartTime + ":" + "00";
                company.BreakEndTime = r.BreakEndTime + ":" + "00";
                company.Interval = r.Interval;
                company.AppointmentStartTime = r.AppointmentStartTime;
                company.AppointmentEndTime = r.AppointmentEndTime;
                getappointmentlist.AppointmentList.Add(company);

}

    return getappointmentlist;
           }



Answers (2)