【问题标题】:Need webService in JSON without <string> / XML需要没有 <string> / XML 的 JSON 格式的 webService
【发布时间】:2015-06-20 22:19:11
【问题描述】:

我正在使用 LINQ to SQL 在 C#/ASP.net 中创建一个 WebService Json Parser。它正在工作,但我的 JSON 正在返回,如下所示。

<string>[{"cli_id":1,"nome":"Joe"},{"cli_id":2,"nome":"Gary"},{"cli_id":3,"nome":"Ash"},{"cli_id":4,"nome":"Pedro"},{"cli_id":5,"nome":"Marcos"}]</string>

我不想要字符串标签。

我正在尝试这段代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {


    }

    DataClassesDataContext dados = new DataClassesDataContext();


    [WebMethod]
    public string getCustomers() {

        var json = "";

        var clientes = from result in dados.clientes select result;

        JavaScriptSerializer jss = new JavaScriptSerializer();

        json = jss.Serialize(clientes);

        return json;

    }

}

它返回一个 JSON,但带有 XML 和 &lt;string&gt; &lt;/string&gt;

如何删除 XML?

【问题讨论】:

标签: c# asp.net xml json web-services


【解决方案1】:

我将代码更改为:

 [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public void getCustomers() {

  //  var json = "";
    Context.Response.Clear();
    Context.Response.ContentType = "application/json"; 

    var clientes = from result in dados.clientes select result;

    JavaScriptSerializer jss = new JavaScriptSerializer();

    Context.Response.Write(jss.Serialize(clientes));


}

【讨论】:

    【解决方案2】:

    试试这个代码。安装 Newtonsoft.Json 包。

    代码

    public class HelloWorldData
        {
            public String Message;
        }
    
        [WebMethod]
        public void getCustomers()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            HelloWorldData data = new HelloWorldData();
            string sql = "SQL_QUERTY";
            SqlDataAdapter da = new SqlDataAdapter(sql, System.Configuration.ConfigurationManager.AppSettings["CONNECTION_STRING"]);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Context.Response.Write(JsonConvert.SerializeObject(ds, Newtonsoft.Json.Formatting.Indented));
        }
    

    【讨论】:

      【解决方案3】:
          [WebMethod]
          [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
          public void getCustomers()
          {
              var clientes = from result in dados.clientes select result;
      
              Context.Response.Clear();
              Context.Response.ContentType = "application/json";
              Context.Response.Write(JsonConvert.SerializeObject(clientes));  
          }
      

      现在,当您创建 Web 方法后,使用此 ajax 调用从 Web 服务获取数据

      var url = "YourURL_of_Webservice"; // example : "http://localhost:54028/webservice.asmx/getCustomers"
      
      $.ajax({
        type: "POST",
        url: url,
        success: function (data) {
          // YOUR CODE FOR SUCCESS BODY
          console.log(data)
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
           console.log(xmlHttpRequest.responseText);
           console.log(textStatus);
           console.log(errorThrown);
        }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-15
        • 1970-01-01
        • 1970-01-01
        • 2015-08-20
        • 2013-07-18
        • 2015-12-22
        • 1970-01-01
        相关资源
        最近更新 更多