Restful Wcf Service不以精确的json形式返回对象

时间:2016-12-01 06:09:44

标签: c# android json rest wcf

我使用框架(4.5)开发了Restful WCF服务。 Android客户端正在使用此服务。

我的Web.Config是:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="httpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>

          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="WcfAndroid.Service1">
        <endpoint address=""
            behaviorConfiguration="httpBehavior"
            binding="webHttpBinding"
            contract="WcfAndroid.IService1" />

      </service>
    </services>
    <protocolMapping>
        <add binding="webHttpBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
     <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

我的界面IService1:

<OperationContract()> _
<WebGet(UriTemplate:="/GetEmp/{empid}", ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
Function GetEmp(ByVal EmpId As String) As DataTable

<OperationContract()> _
<WebGet(UriTemplate:="/GetEmpList/{empid}", ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
Function GetEmpList(ByVal empid As String) As Employee

我的服务:

Public Function GetEmp(ByVal EmpId As String) As DataTable Implements IService1.GetEmp
    Dim table As New DataTable("mytable")
    table.Columns.Add("Result", GetType(String))
    table.Columns.Add("acc", GetType(String))
    table.Columns.Add("name", GetType(String))
    table.Columns.Add("paid", GetType(Double))
    '' Using EmpId for Fetching data from Database
    table.Rows.Add("True", EmpId, "Employee1", 5000)
    table.Rows.Add("True", "2", "Employee2", 2000)
    Return table
End Function
Public Function GetEmpList(empid As String) As Employee Implements IService1.GetEmpList
    ' Using EmpId for Fetching data from Database
    ' For Testing Manual Entry is Done

    Dim EmpKey As String = """2016/Emp""/12"

    Return New Employee With {.EmpID = empid, .EmpKey = EmpKey, .EmpName = "EmployeeName", .EmpPay = "20000"}
End Function

<DataContract()>
Public Class Employee
    <DataMember()>
    Public Property EmpID As String

    <DataMember()>
    Public Property EmpKey As String

    <DataMember()>
    Public Property EmpName As String

    <DataMember()>
    Public Property EmpPay As Decimal
End Class

除了EmpKey,调用 GetEmpList 时,每件事都很好。它包含许多特殊字符,如斜杠,反斜杠,DoubleQuotes。在上面的代码中,我手动发送了“2016 / Emp”/ 12 的EmpKey 但它没有返回确切的关键数据。返回的输出是

  

{ “的EmpID”: “1010”, “EmpKey”: “\” 2016 / EMP \ “/ 12”, “EmpName”: “EmployeeName”, “EmpPay”:20000}

其中EmpKey不正确。

如何将实际数据发送到Android客户端?我在android中使用HttpURLConnection。

1 个答案:

答案 0 :(得分:0)

你可以从JsonObject获取EmpKey的值。它可以解析为:

 JSONObject jobj = new JSONObject(respons);
 String EmpKey = jobj.getString("EmpKey");