【问题标题】:generate JSON for the parent child rows. parent child depth is dynamic为父子行生成 JSON。父子深度是动态的
【发布时间】:2013-04-28 04:21:54
【问题描述】:

我想生成一个代表父子层次结构的 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"
                }
              ]
          }
       ]
      }
    }

【问题讨论】:

  • 你用的是什么应用服务器?
  • 人员工具。它是 peoplesoft 的专有工具。

标签: jquery sql json oracle recursive-query


【解决方案1】:

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

希望对你有帮助。

【讨论】:

  • 感谢您的回复。但由于我不懂java,如果你能为逻辑写更多的描述,我将不胜感激,这样我就可以在我的服务器端尝试这个。
猜你喜欢
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
相关资源
最近更新 更多