Friday 25 March 2011

How To Use JSON Datasource (in WebService) in Asp.net

JSON
JSON (an acronym for JavaScript Object) is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for most programming languages.
The JSON format was originally specified by Douglas Crockford, The official Internet media type for JSON is application/json. The JSON filename extension is .json.
---------------------------------------------------------
Efficiency
JSON is primarily used for communicating data over the Internet, but has certain inherent characteristics that may limit its efficiency for this purpose. Most of the limitations are general limitations of textual data formats and also apply to XML and YAML. For example, despite typically being generated by an algorithm (by machine), parsing must be accomplished on a character-by-character basis. Additionally, the standard has no provision for data compression, interning of strings, or object references. Compression can, of course, be applied to the JSON formatted data (but the decompressed output typically still requires further full parsing for recognizable keywords, tags and delimiters).
---------------How To Implement  JSON ----------------
Download jquery-1.3.2.min.js  and past it in the js folder in your web application
http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.min.js&downloadBtn
--------------- Page Design
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Jeson Test</title>
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
        function testJson() {
            $.ajax({
                type: "POST",
                url: "Json.asmx/TestJSON",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                $("#jsonResponse").html(msg);
                    var data = eval("(" + msg.d + ")");
                    var t = "<table border=1> <tr>" +
                      "<td> <strong>Name</strong></td> <td> " +
                      "<strong>Company</strong></td> <td> " +
                      "<strong>Address</strong></td> <td> " +
                      "<strong>Phone</strong></td> <td> " +
                      "<strong>Country</strong></td> </tr> ";
                      jQuery.each(data, function(rec) {
                        t = t + " <tr> <td> " + this.Name + "</td> <td> " +
                            this.Company + "</td> <td> " + this.Address +
                             "</td> <td> " + this.Phone + "</td> <td> " + this.Country + "</td> </tr> ";
                    });
                    t = t + " </table> ";
                    $("#jsonDiv").html(t);
                },
                error: function(msg) {
                }
            });
        };
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="testjson" type="button" value="Test JSON Call" onclick="testJson()" />
        <br />
        <div id="jsonDiv" style="display: block;">
        </div>
    </div>
    </form>
</body>
</html>
----------------Web service
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class Json : System.Web.Services.WebService {
    public Json () {}
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string TestJSON()
    {
        Employee[] e = new Employee[3];
        e[0] = new Employee();
        e[0].Name = "Sudipta Sanyal";
        e[0].Company = "Esolz";
        e[0].Address = "Kolkata";
        e[0].Phone = "1204675";
        e[0].Country = "India";
        e[1] = new Employee();
        e[1].Name = "Protom Nandi";
        e[1].Company = "Esolz";
        e[1].Address = "Kolkata";
        e[1].Phone = "1204675";
        e[1].Country = "India";
        e[2] = new Employee();
        e[2].Name = "Tanmay Chak";
        e[2].Company = "Esolz";
        e[2].Address = "Kolkata";
        e[2].Phone = "1204675";
        e[2].Country = "India";
        return new JavaScriptSerializer().Serialize(e);
    }  
}
----------------Employee Class 
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public class Employee
{
    public string Name { get; set; }
    public string Company { get; set; }
    public string Address { get; set; }
    public string Phone { get; set; }
    public string Country { get; set; }
}

2 comments:

  1. thanks Zegaara for your wishes.
    if you have any new idea/work for discus you can share with me

    my email-id=sudiptasany@gmail.com

    ReplyDelete
  2. salam kenal bos. lagi jalan jalan pagi nih

    ReplyDelete

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...