【问题标题】:How to get the response and request in JSON format?如何获取 JSON 格式的响应和请求?
【发布时间】:2012-11-03 04:53:56
【问题描述】:

我没有得到 JSON 格式的输出。这是代码

---- IService1.cs ----

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "currency")]
    List<Employee> GetAllEmployeesMethod();

     [OperationContract]
     string TestMethod();

     [OperationContract]
     [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "players")]
     List<Person> GetPlayers();

}

---- Service1.svc.cs ----

public List<Employee> GetAllEmployeesMethod()
{
    List<Employee> mylist = new List<Employee>();

    using (SqlConnection conn = new SqlConnection("--connection string--"))
    {
        conn.Open();

        string cmdStr = String.Format("Select customercode,CurrencyCode,CurrencyDesc from Currency");
        SqlCommand cmd = new SqlCommand(cmdStr, conn);
        SqlDataReader rd = cmd.ExecuteReader();

        if (rd.HasRows)
        {
            while (rd.Read())
                mylist.Add(new Employee(rd.GetInt32(0), rd.GetString(1), rd.GetString(2)));
        }
        conn.Close();
    }


    return mylist;
}

---- web.config ----

  <?xml version="1.0"?>
  <configuration>
 <appSettings/>
 <connectionStrings/>
 <system.web>
<compilation debug="true" targetFramework="4.0"/>   
<authentication mode="Windows"/>    

<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
 <system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>
 <system.serviceModel>
<services>
  <service name="JSONSerialization.Service1" behaviorConfiguration="EmpServiceBehaviour">
    <!-- Service Endpoints -->
    <endpoint address="" binding="webHttpBinding" contract="JSONSerialization.IService1" behaviorConfiguration="web" >
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="EmpServiceBehaviour">        
      <serviceMetadata httpGetEnabled="true"/>          
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" helpEnabled="true" defaultBodyStyle="Bare"/>          
    </behavior>
  </endpointBehaviors>
  </behaviors>   
  </system.serviceModel>  
  </configuration>

当我运行这个服务时,我得到了数据

但我希望结果为 JSON 格式
示例: {"currencycode":"INR","DESCRIPTION":"Indian Rupees","customercode" : "1001"},....

【问题讨论】:

    标签: c# .net json wcf wcf-ria-services


    【解决方案1】:

    将 [Serialize] 属性添加到您的方法中

    【讨论】:

      猜你喜欢
      • 2014-09-14
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 2019-09-30
      • 1970-01-01
      相关资源
      最近更新 更多