当我使用XMLData
调用方法WebMessageFormat.Xml
时,我会得到这样的回复:
当我使用XMLData
调用方法WebMessageFormat.Json
时,我会得到这样的回复:
WCF代码:
namespace RestService
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string XMLData(string id);
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string JSONData();
}
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string XMLData(string id)
{
return "You requested product " + id;
}
public string JSONData()
{
return "You requested product ";
}
#endregion
}
}
配置文件:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="None" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl">
<endpoint name="jsonEP"
address=""
binding="webHttpBinding"
behaviorConfiguration="json"
contract="RestService.IRestServiceImpl"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="json">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我的代码出了什么问题?
答案 0 :(得分:1)
JSON是key:value对的无序集合的序列化格式,其中':'字符分隔键和值,逗号分隔并通常用大括号(对象),括号(数组)或引号(字符串)括起来)
虽然您对响应的要求是JSON格式,但它也是字符串格式的纯文本!没有要序列化的对象/数组,并且没有响应的键/值对,这就是为什么firebug不会在网络选项卡中显示任何JSON预览
尝试在REST服务中返回一些复杂对象,您将在Firebug调试器中看到JSON响应预览:
public class RestServiceImpl : IRestServiceImpl
{
public JSONResponse JSONData(string id)
{
return new JSONResponse { Response = "You requested product " + id };
}
}
public class JSONResponse
{
public string Response { get; set; }
}