Web Services II: Returning Data in JSON Format
40 likes | 206 Vues
Web Services II: Returning Data in JSON Format. JSON vs XML. JSON is a data format that is easier for client-side programmers to work with It takes just one small change to an existing Web Service method to have it data in JSON format rather than their default XML format.
Web Services II: Returning Data in JSON Format
E N D
Presentation Transcript
JSON vs XML • JSON is a data format that is easier for client-side programmers to work with • It takes just one small change to an existing Web Service method to have it data in JSON format rather than their default XML format
WS_MusicCategories: JSON version [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 classWebService : System.Web.Services.WebService { publicWS_MusicCategories () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod][ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<TO_MusicCategory>GetCategories() { DA_MusicCategoriescategoriesObject = new DA_MusicCategories(); returncategoriesObject.GetCategories()"; } }
To have a Web Service Method return JSON data instead of XML: • Add the following line of code: [ScriptMethod(ResponseFormat = ResponseFormat.Json)] This line must be added immediately before the web method declaration (see example code on last slide.