65.9K
CodeProject is changing. Read more.
Home

represnt a object data in JSON fromat

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (1 vote)

Feb 21, 2012

CPOL
viewsIcon

8907

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.
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.
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
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.
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.
    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.
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
        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.