问题
I want to generate a JSON which represent parent child hierarchy. i want to minimize looping on server side to get below expected JSON structure. is it possible to get data from a sql statement, which i can use in server side (with minimum loop) to generate this JSON. i have to support Oracle, DB2, SYBASE,SQL Server etc. below is my table structure and respective sample data
CREATE TABLE TABLEA
(
SCEID VARCHAR(10),
Group_Step VARCHAR(100)
);
CREATE TABLE TABLEB
(
SCEID VARCHAR(10),
Group_Step VARCHAR(100),
Parent_step VARCHAR(100)
);
--- FOR TABLEA--WHICH stores all the group_step
insert into TABLEA values('0000000001','ALLOC1');
insert into TABLEA values('0000000001','ASDF');
insert into TABLEA values('0000000001','BENEFITS');
insert into TABLEA values('0000000001','COPY_BUDG');
insert into TABLEA values('0000000001','CRRNT_PER');
insert into TABLEA values('0000000001','GL_TO_PC');
insert into TABLEA values('0000000001','OVERHEAD');
insert into TABLEA values('0000000001','PC_TO_PC');
--for child(group_step) and parent.. THIS table will have data for the rows having parent child.
insert into TABLEB values('0000000001','BENEFITS','ASDF');
insert into TABLEB values('0000000001','COPY_BUDG','BENEFITS');
insert into TABLEB values('0000000001','GL_TO_PC','COPY_BUDG');
insert into TABLEB values('0000000001','OVERHEAD','CRRNT_PER');
insert into TABLEB values('0000000001','OVERHEAD','GL_TO_PC');
and my Expected JSON format is below and wanted to achieve below format in minimum loops
{
"d": {
"total": 0,
"page": 0,
"records": 0,
"rows": [
{
"id": "1",
"Exclude": "0",
"Groupstep": "PC_TO_PC",
"Version": "0",
"Columnnum": "1",
"Child": [
{}
]
},
{
"id": "2",
"Exclude": "0",
"Groupstep": "OVERHEAD",
"Version": "0",
"Columnnum": "1",
"Child": [
{}
]
},
{
"id": "3",
"Exclude": "0",
"Groupstep": "BENEFITS",
"Version": "1",
"Columnnum": "1",
"Child": [
{
"id": "301",
"Groupstep": "ALLOC1",
"Version": "0",
"Columnnum": "2",
"child": [
{
"id": "3011",
"Groupstep": "ALLOC2",
"Version": "0",
"Columnnum": "3"
}
]
},
{
"id": "302",
"Groupstep": "PC_WIP",
"Version": "0",
"Columnnum": "2"
}
]
},
{
"id": "4",
"Exclude": "0",
"Groupstep": "FRA_LOC_IU",
"Version": "0",
"Columnnum": "1",
"Child": [
{}
]
},
{
"id": "5",
"Exclude": "0",
"Groupstep": "NEXT_YEAR",
"Version": "0",
"Columnnum": "2",
"Child": [
{
"id": "501",
"Groupstep": "FRA_LOC_IU",
"Version": "0",
"Columnnum": "3"
},
{
"id": "502",
"Groupstep": "FRA_LOC_IU1",
"Version": "0",
"Columnnum": "3"
}
]
},
{
"id": "6",
"Exclude": "0",
"Groupstep": "CRRNT_PER",
"Version": "0",
"Columnnum": "2",
"Child": [
{
"id": "601",
"Groupstep": "ACT_BD_ACT",
"Version": "0",
"Columnnum": "3"
},
{
"id": "602",
"Groupstep": "CRRNT_PER",
"Version": "0",
"Columnnum": "3"
}
]
}
]
}
}
回答1:
Follow up from comment:
class Thing {
Integer id;
String name;
String type;
Integer[] children;
public String printMe(Map<Integer, Thing> allThings) {
String ret = "... format json stuff here"; // if there are children then add the key name else format the json key:value pairs of the leaf
for(Integer childId in children) {
Thing child = allThings.get(childId);
ret += child.printMe(allThings);
}
ret += "Format json stuff here";
return ret;
}
};
I hope that helps.
来源:https://stackoverflow.com/questions/16364413/generate-json-for-the-parent-child-rows-parent-child-depth-is-dynamic