为父子行生成JSON.亲子深度是动态的 [英] generate JSON for the parent child rows. parent child depth is dynamic

查看:132
本文介绍了为父子行生成JSON.亲子深度是动态的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

并且我的Expected JSON格式在下面,并且希望在最少的循环中实现以下格式

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

推荐答案

关注 来自评论 :

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

我希望能帮上忙.

这篇关于为父子行生成JSON.亲子深度是动态的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆