Monday, August 20, 2012

Working with JSON

JSON (JavaScript Object Notation) is one lightweight data exchange format.

You don’t need to download an additional library to serialize/deserialize your objects to/from JSON. Since .NET 3.5, .NET can do it natively.
Add a reference to your project to “System.Web.Extensions.dll”
File->New->Website
Add a Class(Static.cs)
Now write the Following Code in Static.cs 
using System;
/// <summary>
    public String Technologies()
    public ArrayList EmpDetails()
    public string JsonString()
Now Add New Item
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
Default.aspx.cs
using System.Web.UI;
public partial class Default3 : System.Web.UI.Page
Now Run
Paste the following output and Click on Viewer
using System.Web.UI;
public partial class Default3 : System.Web.UI.Page

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections;

/// Summary description for Static
/// </summary>
public class Static
{
    JavaScriptSerializer jss = new JavaScriptSerializer();

    {
        string jsonString = string.Empty;
        string[] courses = { "asp.net", "sqlserver", "silverlight", "sharepoint", "iPhone", "vb.net", "java", "php" };
        Dictionary<string, string[]> dicEmpInfo = new Dictionary<string, string[]>();
        dicEmpInfo.Add("Technologies", courses);
        jsonString = jss.Serialize(dicEmpInfo);
        return jsonString;
    }

    {
        Dictionary<string, string> dicpersonalinfo = new Dictionary<string, string>();
        dicpersonalinfo.Add("First Name", "Uday");
        dicpersonalinfo.Add("LastName", "Kumar");
        Dictionary<string, string> dicpersonalInfo2 = new Dictionary<string, string>();
        dicpersonalInfo2.Add("FirstName", "Anvesh");
        dicpersonalInfo2.Add("LastName", "G");
        Dictionary<string, string> dicpersonalInfo3 = new Dictionary<string, string>();
        dicpersonalInfo3.Add("FirstName", "Sunitha");
        dicpersonalInfo3.Add("LastName", "Kumari");
        Dictionary<string, string> dicpersonalInfo4 = new Dictionary<string, string>();
        dicpersonalInfo4.Add("FirstName", "Badri");
        dicpersonalInfo4.Add("LastName", "Nath");
        ArrayList empDetails = new ArrayList();
        empDetails.Add(dicpersonalinfo);
        empDetails.Add(dicpersonalInfo2);
        empDetails.Add(dicpersonalInfo3);
        empDetails.Add(dicpersonalInfo4);
        //string strJson = jss.Serialize(empDetails);
        return empDetails;
    }

    {
        string jsonString1 = string.Empty;
        ArrayList arr = EmpDetails();
        jsonString1 = jss.Serialize(arr);
        return jsonString1;
    }
}



<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>


using System.Web.UI.WebControls;

{
    protected void Page_Load(object sender, EventArgs e)
    {
        Static obj = new Static();
        string str = obj.Technologies();
        Response.Write(str);
    }
}


Now Go to 




For Second Method in static.cs

Default.aspx.cs


using System.Web.UI.WebControls;

{
    protected void Page_Load(object sender, EventArgs e)
    {
        Static obj = new Static();
        string str = obj.Technologies();
       // Response.Write(str);
        string str1 = obj.JsonString();
        Response.Write(str1);
    }
}


copy the output and 
Now Go to 








No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...