What is JSON format?
JSON is a lightweight data-interchange format. Like XML, it is human-readable, platform independent, and enjoys a wide availability of implementations. JSON is a subset of the object literal notation of JavaScript. Data represented in JSON can be parsed by JavaScript easily, so it is ideal for AJAX based web applications.Why should you care about JSON?
If a Web Service returns XML data, then the data to be passed from the server to the client is larger than what it would be in the case of JSON. So, using JSON, we only need to pass fewer amounts of data. Also, XML parsing on client side is cumbersome and costly in the perspective of processing. If there’s another easier way, then why should not we use that?
Let’s assume the following XML data is returned by the Web Service.

<Product>
<ProductID>1</ProductID>
<ProductCode>p_1</ProductCode>
<ProductName>a Product 1</ProductName>
</Product>
To parse data on the client-side, you’ll need to use JavaScript XML features.On the other hand, if the Web Service returns JSON format, then the same data is represented as:
{"ProductID":1,"ProductName":"a Product 1"}
Changes in Web Service to make it JSON enabled:
1. Make The Object Serializable:
For serializing our objects in JSON, we’ll use a data contract serializer, System.Runtime.Serialization.Json.DataContractJsonSerializer
. There’s another serializer called System.Web.Script.Serialization.JavaScriptSerializer
which also can be used for JSON serialization, but this class was deprecated in .NET Framework 3.5 and undeprecated again in .NET Framework 3.5 SP1. Both JavascriptSerializer
and DataContractJsonserializer
have their own benefits.

2. Prepare the web service method to return JSON formatted data:
The Web Service method which will return the JSON data needs to apply the attribute [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
return JSON formatted data. Also, the Web Service’s method return type will always be string. Which will tell the client that the Web Service will

See the following sample Example Program...
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Script.Serialization; using System.Web.Script.Services; using System.Data; using System.Data.SqlClient; /// <summary> /// Summary description for WebService /// </summary> [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 class WebService : System.Web.Services.WebService { public WebService () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } /* The Solution starts from here*/ [WebMethod(Description = "Gets the Account Details.")] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string GetAccountdetailsByName(string name) { SqlConnection objConnection = new SqlConnection("Data Source=WKS98\\SQLEXPRESS;Initial Catalog=sample;Integrated Security=True"); SqlCommand objCommand = new SqlCommand("SELECT * FROM accounts WHERE AccName LIKE '%" + name + "%' ORDER BY AccountID;", objConnection); DataSet objDataSet = new DataSet(); SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand); objDataAdapter.Fill(objDataSet, "names"); objConnection.Close(); // Create a multidimensional jagged array string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][]; int i = 0; foreach (DataRow rs in objDataSet.Tables[0].Rows) { JaggedArray[i] = new string[] { rs["AccountID"].ToString(), rs["AccName"].ToString() }; i = i + 1; } // Return JSON data JavaScriptSerializer js = new JavaScriptSerializer(); string strJSON = js.Serialize(JaggedArray); return strJSON; } } |
No comments:
Post a Comment