Datasource to a Webservice JSON

4 posts, 0 answers
  1. George
    George avatar
    18 posts
    Member since:
    Aug 2007

    Posted 19 Apr 2012 Link to this post

    I am trying to use a ASP.NET Webservice with KendoUI Datasource. For example:

    $("#grid").kendoGrid({
        dataSource: {
            data:airports,
            schema: {
                data: 'd'
            }
        },
        columns: [
            {
                field: "airportName",
                 
            },
            {
                field: "airportCode"
            }
        ]
    });​

    The problem is that the json being returned is:
    d: "[{"airportName":"BDL:Bradley International Airport, CT","airportCode":"BDL"},{"airportName":"HVN:Tweed New Haven Regional Airport, CT","airportCode":"HVN"},{"airportName":"EWR:Newark Liberty Intl, NJ","airportCode":"EWR"},{"airportName":"HPN:Whiteplains Airport,NY","airportCode":"HPN"},{"airportName":"JFK:John F. Kennedy Intl,NY","airportCode":"JFK"},{"airportName":"LGA:La Guardia Airport,NY","airportCode":"LGA"}]
    };"

    Note the quote after the d:
    This is being added by the Webservice because I am using Newtonsoft.Json.JsonConvert.SerializeObject to create the json. I am using Newtonsoft because I have not found a was to serialize a generic List object.

    So the question is:
    1. How do I get DataSource to work with the quoted results?
    or
    2. How do I get the webservice to not to quote the results?

    The Webservice code is:
    [WebMethod()]
    public string GetAirportList()
    {
        List<Airport> Airports = new List<Airport>();
        Airports.Add(new Airport {
            airportCode = "BDL",
            airportName = "BDL:Bradley International Airport, CT"
        });
        Airports.Add(new Airport {
            airportCode = "HVN",
            airportName = "HVN:Tweed New Haven Regional Airport, CT"
        });
        Airports.Add(new Airport {
            airportCode = "EWR",
            airportName = "EWR:Newark Liberty Intl, NJ"
        });
        Airports.Add(new Airport {
            airportCode = "HPN",
            airportName = "HPN:Whiteplains Airport,NY"
        });
        Airports.Add(new Airport {
            airportCode = "JFK",
            airportName = "JFK:John F. Kennedy Intl,NY"
        });
        Airports.Add(new Airport {
            airportCode = "LGA",
            airportName = "LGA:La Guardia Airport,NY"
        });
        return Newtonsoft.Json.JsonConvert.SerializeObject(Airports);
    }

    Please help. (I've been tearing my hair out for two days on this).

    -George


  2. George
    George avatar
    18 posts
    Member since:
    Aug 2007

    Posted 20 Apr 2012 Link to this post

    I found the problem with my WebMethod:
    Changing the return datatype to an array of my object type removed the need to NewtonSoft:

    [WebMethod()]
    public Airport[] GetAirportList()
    {
        List<Airport> Airports = new List<Airport>();
        Airports.Add(new Airport {
            airportCode = "?",
            airportName = ""
        });
        Airports.Add(new Airport {
            airportCode = "BDL",
            airportName = "BDL:Bradley International Airport, CT"
        });
        Airports.Add(new Airport {
            airportCode = "HVN",
            airportName = "HVN:Tweed New Haven Regional Airport, CT"
        });
        Airports.Add(new Airport {
            airportCode = "EWR",
            airportName = "EWR:Newark Liberty Intl, NJ"
        });
        Airports.Add(new Airport {
            airportCode = "HPN",
            airportName = "HPN:Whiteplains Airport,NY"
        });
        Airports.Add(new Airport {
            airportCode = "JFK",
            airportName = "JFK:John F. Kennedy Intl,NY"
        });
        Airports.Add(new Airport {
            airportCode = "LGA",
            airportName = "LGA:La Guardia Airport,NY"
        });
        return Airports.ToArray;
    }

    Now the returned JSON is not quoted.

    -George
  3. Kendo UI is VS 2017 Ready
  4. Engifaar
    Engifaar avatar
    15 posts
    Member since:
    May 2012

    Posted 01 Jun 2012 Link to this post

    @  George Sheehy  thank you for your post... at last i solver it...
  5. Isaac
    Isaac avatar
    1 posts
    Member since:
    Jun 2012

    Posted 02 Jun 2012 Link to this post

    really nice information given here, nice job guys, really being helpful.

    ---------------------------
    Isaac.
Back to Top
Kendo UI is VS 2017 Ready