3

I'm creating a WebService Json Parser in C#/ASP.net using LINQ to SQL. It is working, but my JSON is returning as below.

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

I don't want the string tags.

I'm trying this code:

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;

    }

}

It's returning a JSON, but with the XML and <string> </string>

How do I remove the XML?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
user17245
  • 207
  • 1
  • 8

3 Answers3

3

I changed my code to:

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


}
user17245
  • 207
  • 1
  • 8
3

Try this code. Install Newtonsoft.Json package.

CODE

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));
    }
Bhavanaditya
  • 548
  • 3
  • 8
  • 23
2
    [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));  
    }

Now when you have created the web method use this ajax call to fetch data from web service

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);
  }
});
Saad Mujeeb
  • 106
  • 1
  • 3