1

親子階層を表す JSON を生成したい。サーバー側でのループを最小限に抑えて、予想される JSON 構造を下回りたいと考えています。この JSON を生成するためにサーバー側で (最小限のループで) 使用できる SQL ステートメントからデータを取得することは可能ですか。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 に答える