[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を取得したいです。ここに私の FareDataModel オブジェクトがあります
[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;
}
投稿の1つで説明されているすべての変更を試しましたが、何も機能していません. 以下のようにコードを変更して文字列として返すと
[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>