Web Services, where XML is difficult to process on the client side. Also, XML is larger in size than its corresponding JSON representation. For each property, XML has two tags: opening and closing. On the other hand, JSON has the property name only once.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. For your information, JSON does not have any support for datetime as JavaScript doesn't have its own datetime data type. Rather, datetime in JavaScript is an object. You can read here details on how JSON datetime is handled in AJAX.
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.
<Employee>
<EmpID>100</EmpID>
<Name>Aamir Hasan</Name>
<Address>Studentacad.com</Address>
</Employee>
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:
{"EmpID":100,"Name":Aamir Hasan,"Address": Studentacad.com}
we can see that the JSON representation is much smaller in size than its equivalent XML representation. Also, we’ll see later how easy it is to parse JSON formatted data.
Define JSON serializer in web.config
he JSON serializer described later is only supported in .NET framework 3.5
<section name="jsonSerialization"
type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions,
Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
requirePermission="false" allowDefinition="Everywhere"/>
By default, the ScriptJsonSerializer limits the JSON string to a length of 102400 (UTF-8) characters.
If you need to change the maximum length of the JSON string, then change the maxJsonLenght property of JsonSerialization.
In the section shown below, the max length is changed to 80000 characters:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="80000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
Make the object serializable
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetProductsJson(string prefix)
{
List<Product> products = new List<Product>();
if (prefix.Trim().Equals(string.Empty, StringComparison.OrdinalIgnoreCase))
{
products = ProductFacade.GetAllProducts();
}
else
{
products = ProductFacade.GetProducts(prefix);
}
//yourobject is your actula object (may be collection) you want to serialize to json
DataContractJsonSerializer serializer = new DataContractJsonSerializer(products.GetType());
//create a memory stream
MemoryStream ms = new MemoryStream();
//serialize the object to memory stream
serializer.WriteObject(ms, products);
//convert the serizlized object to string
string jsonString = Encoding.Default.GetString(ms.ToArray());
//close the memory stream
ms.Close();
return jsonString;
Original Content
http://www.codeproject.com/KB/webservices/JsonWebServiceJQuery.aspx
Author:
Aamir Hasan औथोर:
आमिर हसन أثر
أمير حسن .
064bbcf5-4371-4857-9761-22f5cf178b3c|0|.0
Ajax, ALL, asp.net, Javascript, JQuery
json, asp.net, ajax, javascript,