WCF 服务不返回 JSON 中的完整对象
本文关键字:对象 JSON 服务 返回 WCF | 更新日期: 2023-09-27 18:31:19
[OperationContract]
public FareDataModel GetFareAndDistanceJson(string source, string destination, string country, string city)
{
return FareManager.GetFare(source, destination, country, city);
}
我的 svc 类中有上述方法。但这不能完全以 Json 格式发送数据。
{
"d": {
"__type": "FareDataModel:#MeterupAutoTaxiWebService",
"Fares": [
{
"__type": "VehicleFare:#MeterupAutoTaxiWebService"
}
]
}
}
但是当我将其作为字符串返回时,我以字符串的形式获取数据。我真的很想得到json。这是我的票价数据模型对象
[DataContract]
public class FareDataModel
{
public string SourceAddress;
public string DestinationAddress;
public int Duration;
public int Distance;
public string Error;
[DataMember]
public List<VehicleFare> Fares = new List<VehicleFare>();
}
[DataContract]
public class VehicleFare
{
public string PhoneNo;
public string Vehicle;
public double Fare;
}
我尝试了其中一篇文章中解释的所有更改,但没有任何内容对我有用。当我通过修改我的代码以字符串返回时,如下所示
[OperationContract]
public string GetFareAndDistanceJson(string source, string destination, string country, string city)
{
return new JavaScriptSerializer().Serialize(FareManager.GetFare(source, destination, country, city));
}
我得到以下结果
{
"d": "{'"SourceAddress'":'"Majestic, Bangalore, Karnataka, India'",'"DestinationAddress'":'"Marathahalli Market, Bangalore, Karnataka, India'",'"Duration'":2475,'"Distance'":19872,'"Error'":null,'"Fares'":[{'"PhoneNo'":null,'"Vehicle'":'"Auto'",'"Fare'":238}]}"
}
我真的需要一个真正的 json 响应。
这是我的班级
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public string GetFareAndDistanceJson(string source, string destination, string country, string city)
{
return new JavaScriptSerializer().Serialize(FareManager.GetFare(source, destination, country, city));
}
// Add more operations here and mark them with [OperationContract]
}
请帮帮我。
我的网络配置
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="MeterupAutoTaxiWebService.Service1">
<endpoint address="" behaviorConfiguration="MeterupAutoTaxiWebService.Service1AspNetAjaxBehavior"
binding="webHttpBinding" contract="MeterupAutoTaxiWebService.Service1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MeterupAutoTaxiWebService.Service1AspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<connectionStrings>
<add name="MeterupAutoTaxiEntities" connectionString="metadata=res://*/MeterupTaxiAutoEntities.csdl|res://*/MeterupTaxiAutoEntities.ssdl|res://*/MeterupTaxiAutoEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=MeterupAutoTaxi;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
在将 [DataMember] 添加到每个后工作。当我请求 XML 时,它返回了有效的 XML,但没有添加它。但我必须添加它以返回 json