Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要JSON格式的Web服务,而不需要/XML_C#_Asp.net_Xml_Json_Web Services - Fatal编程技术网

C# 需要JSON格式的Web服务,而不需要/XML

C# 需要JSON格式的Web服务,而不需要/XML,c#,asp.net,xml,json,web-services,C#,Asp.net,Xml,Json,Web Services,我正在使用LINQ to SQL在C/ASP.net中创建一个WebService Json解析器。它正在工作,但我的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> 我不要字符串标签 我正在尝试

我正在使用LINQ to SQL在C/ASP.net中创建一个WebService Json解析器。它正在工作,但我的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和

如何删除XML?

我将代码更改为:

 [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));


}
现在,当您创建了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);
  }
});

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

代码


因为您返回的是字符串,所以它将该字符串包装为XML。请检查此项-非常感谢。我在论坛上搜索了很多,但是没有找到。
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);
  }
});
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));
    }