2015-09-26 45 views
-1

您好,我想問一下如何讓webservice在沒有XML標籤的Visual Studio(ASP.NET)中返回JSON?如何創建返回JSON數據的WebService

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

+0

爲什麼不使用webapi restful? –

+0

任何MVC控制器(或webAPIController)都可以爲您做到這一點 –

+0

Web API使用JSON格式。 –

回答

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); 
}