【问题标题】:WCF service for android with json带有 json 的 Android 的 WCF 服务
【发布时间】:2016-11-30 07:32:29
【问题描述】:

我创建了一个由 android 使用的 WCF 服务。它返回一个json字符串,但服务器代码发送的实际字符串数据与访问时不同。

我的网络配置

<?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>

还有我的 WCF 服务接口:

<ServiceContract()>
Public Interface IService1

     <OperationContract()> _
        <WebGet(UriTemplate:="GetText", BodyStyle:=WebMessageBodyStyle.WrappedRequest, responseformat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
        Function GetText() As String

 End Interface

我正在从服务中返回一个常量字符串进行测试:

Public Function GetText() As String Implements IService1.GetText
    '' GetjsonString()   returns   {"id": 100,"name": "item X","active": true}
    Return GetjsonString()
End Function

GetjsonString() 函数返回字符串 {"id": 100,"name": "item X","active": true}

但是当服务被调用时,它会返回:

{\"id\": 100,\"name\":\"item X\",\"active\": true}

我不知道为什么它没有返回实际的字符串?

【问题讨论】:

  • 是可以解析的有效数据。它将按预期工作。
  • 当我解析字符串时,它会在字符串 \"id\" 中返回额外的斜杠。如输出所示
  • 在发送字符串时尝试发送对象。

标签: c# android json vb.net wcf


【解决方案1】:

我不太熟悉语言,所以我不太确定该功能是如何工作的。做这样的事情:

string.replaceAll("\\", "")

去掉“\”;

你的字符串发生的事情是它已经转义了“。

如果这还不够,而且显然这种方法有缺陷,类似问题有一个答案: wcf forces chars escaping in response

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 2011-07-14
    • 1970-01-01
    相关资源
    最近更新 更多