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?

3
  • Since you are returning string, it is wrapping that string in XML . Commented Apr 15, 2015 at 5:45
  • Check this - stackoverflow.com/questions/19563641/… Commented Apr 15, 2015 at 5:53
  • Thanks a lot. I've been searching a lot for this in the forum, but I didn't found it. Commented Apr 15, 2015 at 13:34

3 Answers 3

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


}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.