Consuming a .NET JSON web service

5 posts, 1 answers
  1. David
    David avatar
    32 posts
    Member since:
    May 2012

    Posted 08 Jun 2012 Link to this post


    I'm building a .NET web service that my application will consume. I've built the service to deliver JSON data. Using the .NET serializer the data looks like this:

    [{"title":"Stuff","value":"Here's some stuff"}]

    Here's the web service:

    [WebService(Namespace = "")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        public class websiteWebService : System.Web.Services.WebService
            [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public string GetStuff(int stuff_id)
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["stuff"].ConnectionString);
                SqlCommand command = new SqlCommand("o_get_stuff", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@stuff_id", stuff_id);
                SqlDataReader dr = command.ExecuteReader();
                DataTable dt = new DataTable("Stuff");
                List<Dictionary<string, object>> rows = new
    List<Dictionary<string, object>>();
                Dictionary<string, object> row = new
    Dictionary<string, object>();
                foreach (DataRow datarow in dt.Rows)
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                        row.Add(col.ColumnName, datarow[col]);
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string jsonData = serializer.Serialize(rows);
                return jsonData;

    Here is my javascript as it stands:

    <script type="text/javascript">
            $(document).ready(function () {
                    dataTextField: "title",
                    dataValueField: "value",
                    dataSource: {
                        transport: {
                            read: {
                                contentType: "application/json",
                                type: "POST",
                                url: "http://localhost:23035/Services/websiteWebService.asmx/GetStuff",
                                data: {
                                    stuff_id: "0"

    The problem I have is that the data array is wrapped in a <string>. How do I modify my datasource (or the web service) to provide just the array so that I can directly read the "title" and "value"? I'd like to drop something into the datasource schema but I don't know what will strip off the <string>.

  2. Answer
    Atanas Korchev
    Atanas Korchev avatar
    8462 posts

    Posted 11 Jun 2012 Link to this post


     You should not explicitly serialize your result as JSON - the web service will do this automatically for you. You can check this demo which shows how to consume an ASMX web service with Kendo. You may also find this blog post useful: 

    All the best,

    Atanas Korchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. David
    David avatar
    32 posts
    Member since:
    May 2012

    Posted 11 Jun 2012 Link to this post

    Awesome advice! Thanks!

    I've got the basics of this but I'm hung up on the details. In my case I don't have a datamodel so serializing the objects can't be done exactly as in the example. My data is simple: just the datatable returned from a sql stored procedure. I've tried a bunch of means of serializing but they either fail as a circular reference or lose the field names so the js can't get at the data.

    Any advice on serializing a dataset without a datamodel?


    Booyah! Here's how I managed to serialize my datatable:
    List<Template> list = new List<Template>();
    Template tmplt = new Template();
    foreach (DataRow row in dt.Rows)
      tmplt.title = (string)row["title"];
      tmplt.value = (string)row["value"];
    return list.ToList();

    Just had to add a simple class to the webservice (public class Template) and voila!
  4. Don
    Don avatar
    38 posts
    Member since:
    Oct 2006

    Posted 12 Jul 2012 Link to this post

    I am also using an .asmx web service that returns a List<SeriesData>
    (a simple class with 3 properties of type string)

    I can get this to work with $.ajax({}), but not using the kendo DataSource/transport.
    I am making a call to an external service but it has been configured to allow cross domain calls (again, works with jQuery)
    I read (alot) and found that the kendo DataSource is entirely based on $.ajax.

    so, why cant I get it to work using the transport?

    function loadAJAX() {                
           = true;
                         type: 'POST',
                         cache: false,
                         url: remoteHost + "getCustomersFiltered_JSON",
                         contentType: 'application/json; charset=utf-8',
                         data: "{startValue: 'abc'}",
                         dataType: 'json',
                         success: onSuccess,
                         error: onError                   

      function createGridWithTransport()
                            dataSource:  new
                                transport: {
                                    read: {
                                            url:  remoteHost + "getCustomersFiltered_JSON"
                                            , data: {startValue: 'abc'}
                                            //, data: "{startValue: 'abc'}"
                                            , contentType: 'application/json; charset=utf-8'
                                            , type: 'POST'
                                            , dataType: "json"
                                schema: {data: "d"}
                            columns: [
                                {title: 'Name', field:'Name'},
                                {title: 'Key', field:'Key'},
                                {title: 'Value', field:'Value'}
                    alert("no JS errors");
  5. Michael
    Michael avatar
    19 posts
    Member since:
    Oct 2011

    Posted 08 May 2013 Link to this post

    Your webservice is fundamentally flawed. You should be using standard .net objects and returning them; not serializing to JSON. See this article for a great write-up about that VERY common mistake:

    Notice that the results will be wrapped as {d: YOUR_RESULTS} so you'll need to refer to response.d (no need to parse).
Back to Top