This is a migrated thread and some comments may be shown as answers.

Datasource to a Webservice JSON

3 Answers 281 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
George
Top achievements
Rank 1
George asked on 20 Apr 2012, 03:04 AM
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


3 Answers, 1 is accepted

Sort by
0
George
Top achievements
Rank 1
answered on 20 Apr 2012, 12:29 PM
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
0
Engifaar
Top achievements
Rank 1
answered on 01 Jun 2012, 12:45 PM
@  George Sheehy  thank you for your post... at last i solver it...
0
Isaac
Top achievements
Rank 1
answered on 02 Jun 2012, 06:27 PM
really nice information given here, nice job guys, really being helpful.

---------------------------
Isaac.
Tags
Data Source
Asked by
George
Top achievements
Rank 1
Answers by
George
Top achievements
Rank 1
Engifaar
Top achievements
Rank 1
Isaac
Top achievements
Rank 1
Share this question
or