Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Javascript
Tip/Trick

represnt a object data in JSON fromat

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Feb 2012CPOL 8.8K   1  
This tip display the how to convert a object data to a Javascript array(JSON format)
JSON stands for java script object notation. Now in all web applications it becomes a main part. In this discussion I am just giving the basic idea about how a JSON format will be.

In java script a simple array looks like bellow.

JavaScript
var names = [‘test’, 'javascript', 'object',’notation’];
/* to display the above data iterate like bellow */
            for (var i = 0; i < names.length; i++) {
                display +="  :"+ names[i];
            }


A complex object data will be represented in java script array, it becomes JSON notation.


Above is the simple Java script array, where we are stored certain words. Now think I have a class EmployeeAttendance. The structure of this class looks like bellow.

C#
public class EmployeeAttendance
    {
        public string MonthName { get; set; }
        public string WorkingDays { get; set; }
        public string Attendeddays { get; set; }
        public string LeavesTaken { get; set; }
    }


Now I have 3 months history like this

C#
EmployeeAttendance obj1 = new EmployeeAttendance();
            obj1.MonthName = "Jan";
            obj1.WorkingDays = "30Days";
            obj1.LeavesTaken = "0Days";
            obj1.Attendeddays = "30Days";

            obj1 = new EmployeeAttendance();
            obj1.MonthName = "Feb";
            obj1.WorkingDays = "29Days";
            obj1.LeavesTaken = "1Day";
            obj1.Attendeddays = "28Days";

            obj1 = new EmployeeAttendance();
            obj1.MonthName = "Mar";
            obj1.WorkingDays = "31Days";
            obj1.LeavesTaken = "2Days";
            obj1.Attendeddays = "29Days";


So this data if you want to represent in the Javascript array it looks like bellow.

JavaScript
var display = "";

            var myJSONObject = { "santhosh": [
        { "MonthName": "Jan", "WorkingDays": "30days", "Attendeddays": "30days", "LeavesTaken": "0days" },
        { "MonthName": "Feb", "WorkingDays": "29days", "Attendeddays": "28days", "LeavesTaken": "1days" },
        { "MonthName": "Mar", "WorkingDays": "31days", "Attendeddays": "29days", "LeavesTaken": "2days" }
    ]
            };
            for (var i = 0; i < myJSONObject.santhosh.length; i++) {
                display += "Month:" + myJSONObject.santhosh[i].MonthName;
                display += "Working Days:" + myJSONObject.santhosh[i].WorkingDays;
                display += "Attended days :" + myJSONObject.santhosh[i].Attendeddays;
                display += "Leaves Taken:" + myJSONObject.santhosh[i].LeavesTaken;

            }


If you i modified EmployeeAttendance class structure something like bellow.

C#
public class EmployeeAttendance
{
    public string MonthName { get; set; }
    public string WorkingDays { get; set; }
    public string Attendeddays { get; set; }
    public string LeavesTaken { get; set; }
 public string[] AbsentDays { get; set; }

}



Now i want to store the absent dates in each month then object looks like bellow.
C#
EmployeeAttendance obj1 = new EmployeeAttendance();
            obj1.MonthName = "Jan";
            obj1.WorkingDays = "30Days";
            obj1.LeavesTaken = "0Days";
            obj1.Attendeddays = "30Days";
	     obj1.AbsentDays = {}; //empty because no leaves applied


            obj1 = new EmployeeAttendance();
            obj1.MonthName = "Feb";
            obj1.WorkingDays = "29Days";
            obj1.LeavesTaken = "1Day";
            obj1.Attendeddays = "28Days";
 obj1.AbsentDays = {13}; //Feb 13 absent to office


            obj1 = new EmployeeAttendance();
            obj1.MonthName = "Mar";
            obj1.WorkingDays = "31Days";
            obj1.LeavesTaken = "2Days";
            obj1.Attendeddays = "29Days";
     obj1.AbsentDays = {17,25}; //Mar 17 & March 25 absent to office


Then in this case JSON Object likes bellow

JavaScript
    var display = "";

    var myJSONObject = { "santhosh": [
    { "MonthName": "Jan", "WorkingDays": "30days", "Attendeddays": "30days", "LeavesTaken": "0days" , "AbsentDays":[] },
    { "MonthName": "Feb", "WorkingDays": "29days", "Attendeddays": "28days", "LeavesTaken": "1days", "AbsentDays": [13] },
    { "MonthName": "Mar", "WorkingDays": "31days", "Attendeddays": "29days", "LeavesTaken": "2days", "AbsentDays": [17,25] }
]
    };
    for (var i = 0; i < myJSONObject.santhosh.length; i++) {
        display += "Month:" + myJSONObject.santhosh[i].MonthName;
        display += "Working Days:" + myJSONObject.santhosh[i].WorkingDays;
        display += "Attended days :" + myJSONObject.santhosh[i].Attendeddays;
        display += "Leaves Taken:" + myJSONObject.santhosh[i].LeavesTaken;
        for (var j = 0; j < myJSONObject.santhosh[i].AbsentDays.length; j++) {
            display += myJSONObject.santhosh[i].AbsentDays[j]; // to display the dates absent.
        }
    }


So like this any complex object can be convert into JSON fromat.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Semantic Space Technologies
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --