I would like to understand how to proceed with creation of REST based WCF service and entity data model using JSON response. I have followed the instructions listed in :
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
This is the namespace DeviceManagementService_Rest
in the IService1.cs file:
Here. deviceOS is the parameter used to list down different devices running a particular OS.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json, UriTemplate = "json/{deviceOS}", BodyStyle = WebMessageBodyStyle.Bare)]
string JSONData(string deviceOS);
}
This class is created to query the data and convert into JSON format:
public class Service1 : IService1
{
DeviceManEntities dme;
public string JSONData(string deviceOS)
{
dme = new DeviceManEntities();
var result = (from m in dme.DeviceInfoes
select new
{
Name = m.deviceName,
OS = m.deviceOS,
UID = m.deviceUID
}).ToList();
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return serializer.Serialize(result);
}
}
The problem being: I get the response in this format:
"[{\"Name\":\"Galaxy S3\",\"OS\":\"Andriod\",\"UID\":\"101\"},
{\"Name\":\"iPhone 5-D1\",\"OS\":\"iOS\",\"UID\":\"123\"}]"
Where as, ideally I would have expected it to be like this:
"[{"Name":"Galaxy S3","OS":"Android","UID":"101"}
{"Name":"iPhone 5-D1","OS":"iOS","UID":"123"}]"
- How can I remove those "\"s
- Is the implementation of serialization correct ? if no, is there a better way ?