WCF JSON输出正在获得不需要的报价&添加了反斜杠

时间:2014-03-10 07:35:45

标签: c# json wcf

好的,所以我很困惑为什么我正在构建的字符串“神奇地”添加了额外的字符。

首先,我看到了立即窗口中出现的反斜杠:

"[{\"ID\":\"1\",\"F1\":\"lala\",\"F2\":\"hehe\"},{\"ID\":\"2\",\"F1\":\"abc\",\"F2\":\"def\"}]"

但是在Google上看到这些只是“视觉”,并且实际上并不存在于变量值本身时,我尝试了Visualizer文本:

[{"ID":"1","F1":"lala","F2":"hehe"},{"ID":"2","F1":"abc","F2":"def"}]

..好吧,他们不在那里:)

然后我会从Chrome浏览器调用webservce:

{"JSONsampleResult":"[{\"ID\":\"1\",\"F1\":\"lala\",\"F2\":\"hehe\"},{\"ID\":\"2\",\"F1\":\"abc\",\"F2\":\"def\"}]"}

所以不仅反斜杠在那里,而且还在我的方括号周围添加了引号!?

请请有人解释为什么我不能解析一个简单的字符串? 什么是添加所有这些额外的东西?

ServiceRestTable.svc(摘录)

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace netshedWebService_2
{
     public class ServiceRestTable : IServiceRestTable
    {
        private static long EpochTicks = new DateTime(1970, 1, 1).Ticks;

        string ConString = ConfigurationManager.ConnectionStrings["netshedConnectionString"].ConnectionString;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter sda;
        DataTable dt;
        Sample emp = new Sample();

        public String JSONsample(string p_str)
        {
            string myList;
            using (con = new SqlConnection(ConString))
            {
                cmd = new SqlCommand("SELECT ID, F1, F2 FROM ADT.Test", con);
                sda = new SqlDataAdapter(cmd);
                dt = new DataTable("Paging");
                sda.Fill(dt);
                emp.SampleTable = dt;
                myList = FromDataTable(emp.SampleTable);
                return myList;
            }
        }

IServiceRestTable.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace netshedWebService_2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestServiceImpl" in both code and config file together.
    [ServiceContract]
    public interface IServiceRestTable
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/{id}")]
        Sample XMLsample(string id);

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}")]
        String JSONsample(string id);
    }
    [DataContract]
    public class Sample
    {
        [DataMember]
        public DataTable SampleTable
        {
            get;
            set;
        }
    }
}

的Web.config

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="netshedConnectionString" connectionString="xxxxx"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="netshedWebService_2.ServiceRestTable" behaviorConfiguration ="RestServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="netshedWebService_2.IServiceRestTable" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RestServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
            <webHttp/>
        </behavior>
      </endpointBehaviors>      
    </behaviors>    
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

0 个答案:

没有答案