Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
f I have a datatable in the following structure.

HostelName	    FloorName	FlatName	Occupied	Vacant
Building One	Floor A	        A	        2	       2
Building One	Floor A	        B	        0	       4
Building One	Floor A	        C	        0	       4
Building One	Floor A	        D	        0	       4
Building One	Floor A	        E	        0	       4
Building One	Floor B	        F	        0	       4
Building One	Floor B	        G	        0	       4
Building One	Floor B     	H	        0	       4
Building One	Floor B         I	        0	       4
Building One	Floor B         J	        0	       4


I would like to serialize it as a JSON object where the HostelName,FloorName & FlatName columns are the nodes in the JSON object like:



{
                "Building One": {
                    "Floor A": {
                        "A": {
                            "Occupied": "2",
                            "Vacant": "2"
                        },
                        "B": {
                            "Occupied": "0",
                            "Vacant": "4"
                        },
                        "C": {
                            "Occupied": "0",
                            "Vacant": "4"
                        },
                        "D": {
                            "Occupied": "0",
                            "Vacant": "4"
                        },
                        "E": {
                            "Occupied": "0",
                            "Vacant": "4"
                        }
                    },
                    "Floor B": {
                        "F": {
                            "Occupied": "0",
                            "Vacant": "4"
                        },
                        "G": {
                            "Occupied": "0",
                            "Vacant": "4"
                        },
                        "H": {
                            "Occupied": "0",
                            "Vacant": "4"
                        },
                        "D": {
                            "Occupied": "0",
                            "Vacant": "4"
                        },
                        "I": {
                            "Occupied": "0",
                            "Vacant": "4"
                        }
                    }
                }
            };



Please help me to solve it.

Thank you
Subin ks
Posted

See this : http://stackoverflow.com/a/11182071[^]. Its not exactly what you are looking for. You need some modifications.

Regards..
 
Share this answer
 
v2
int i=0;
StringBuilder sbJSON = new StringBuilder();
datatable dt;
sbJSON.Append("[");
if(dt.Rows.count>0)
{
foreach (DataRow dr in dt.Rows)
{
sbJSON.Append("{\"" + dr[0] + "\": ");
sbJSON.Append(",\"" + dr[1] + "\": ");
sbJSON.Append(",\"" + dr[2] + "\": ");
sbJSON.Append("\"" + dr[3] + "\",");
sbJSON.Append("\"" + dr[4] + "\"},");
}
sbJSON.Remove(sbJSON.Length - 1, 1);
}
sbJSON.Append("]");

result = sbJSON.ToString();
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900