Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have written a WCF REST service.My Get method would return a list values..When I execute my xml response is :
XML
<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?
I am using this code:
IService.cs:
C#
[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 :
C#
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;
}

Thanks,
Posted
Updated 7-Sep-12 22:42pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900