簡體   English   中英

Restful Wcf服務不會以准確的json格式返回對象

[英]Restful Wcf Service does not Returns objects in accurate json form

我使用framework(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

調用GetEmpList時 ,除了EmpKey之外,一切都很好。 它包含許多特殊字符,例如斜杠,反斜杠,雙引號。 在上面的代碼中,我手動發送了“ 2016 / Emp” / 12的EmpKey,但是它沒有返回確切的密鑰數據。 返回的輸出是

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

EmpKey不正確。

如何發送實際數據到Android客戶端? 我在android中使用HttpURLConnection。

您可以從JsonObject獲取EmpKey的Value。 它可以解析為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM