如何创建以JSON格式返回数据的WebService

时间:2015-09-26 21:50:59

标签: c# asp.net json web-services

你好我想问一下如何在没有XML标签的情况下让webservice在Visual Studio(ASP.NET)中返回JSON?

   <string xmlns="http://tempuri.org/">{"account":[{"name":"XXXX ","phoneNumber":"5555544 ","email":"sas@sa.com"},{"name":"slh ","phoneNumber":"0565555","email":"1 "}]}

1 个答案:

答案 0 :(得分:0)

要使WCF REST服务(.svc)返回JSON,您需要在合同上添加属性,告诉它返回JSON。请注意,在下面的示例中,响应格式设置为JSON。

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "getEmployee/{id}")]
    Employee GetEmployee(int id);
}