1

我想生成一个代表父子层次结构的 JSON。我想最小化服务器端的循环以低于预期的 JSON 结构。是否可以从 sql 语句中获取数据,我可以在服务器端使用它(具有最小循环)来生成这个 JSON。我必须支持 Oracle、DB2、SYBASE、SQL Server 等。下面是我的表结构和相应的示例数据

  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');

并且我的预期 JSON 格式低于并希望在最小循环中实现以下格式

     {
      "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"
                }
              ]
          }
       ]
      }
    }
4

1 回答 1

0

从评论跟进 :

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;
} 
};

我希望这会有所帮助。

于 2013-05-13T23:31:15.440 回答