Return JSON Response From Web Service In ASP.NET

I hope the basics of web service are clear by now. As we all know Web Service returns XML output by default. But that can be pretty heavy depending upon the data and as the number of hits increases with lots of data the efficiency of the data transfer decreases. JSON as we know is Javascript Object Notation and is very lightweight and has gained a good momentum to use in such scenarios. Developers now prefer JSON over XML response in a Web Service.

Let us create a web Service and see how to return a JSON response from the same.

Open Visual Studio. Create an application. Add Web Service to it. Add the following code in the code behind file of the service.

  1. using System.Web.Script.Serialization;  
  2. using System.Web.Script.Services;  
  3. using System.Web.Services;  
  4. namespace WebServiceXMLtoJSON  
  5. {  
  6.     public class Students   
  7.     {  
  8.         public int StudentId   
  9.         {  
  10.             get;  
  11.             set;  
  12.         }  
  13.         public string Name  
  14.         {  
  15.             get;  
  16.             set;  
  17.         }  
  18.         public int Marks {  
  19.             get;  
  20.             set;  
  21.         }  
  22.     }  
  23.     ///<summary>  
  24.     /// Summary description for TestService  
  25.     ///</summary>  
  26.     [WebService(Namespace = "http://tempuri.org/")]  
  27.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  28.     [System.ComponentModel.ToolboxItem(false)]  
  29.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  30.     // [System.Web.Script.Services.ScriptService]  
  31.     public class TestService: System.Web.Services.WebService   
  32.     {  
  33.         [WebMethod]  
  34.         [ScriptMethod(ResponseFormat = ResponseFormat.Json)]  
  35.         public void GetStudents()   
  36.         {  
  37.             Students[] obj students = new Students[]   
  38.             {  
  39.                 new Students()   
  40.                 {  
  41.                         StudentId = 1,  
  42.                             Name = "NitinTyagi",  
  43.                             Marks = 400  
  44.                     },  
  45.                     new Students()   
  46.                     {  
  47.                         StudentId = 2,  
  48.                             Name = "AshishTripathi",  
  49.                             Marks = 500  
  50.                     }  
  51.             };  
  52.             JavaScriptSerializerjs = newJavaScriptSerializer();  
  53.             Context.Response.Write(js.Serialize(objstudents));  
  54.         }  
  55.     }  
  56. }  
We have defined a Students class and populated some data in the GetStudents() webmethod. Have a look closely we have used the following attribute as the response format because we need JSON format.
  1. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]  
We need to serialize the data by JavaScriptSerializer class. We have created an object of JavaScriptSerializer class and used the serialize method which serializes the data.

Let us check the output of the program.

output

Click on GetStudents and we get the JSON response from the service.

service

We have successfully generated JSON response from a web service.
 
Read more articles on ASP.NET: 


Similar Articles