关于dataTable 生成JSON 树

背景: POSTGRESL + C#  + DHTMLX SUIT

一个表生成一个JSON串,这个不是很麻烦:

1.在数据库(postges)中:  json_agg(row_to_json(t))

2.在C#中直接FOR 循环 拼接是没有问题

这都不是我想要的,

我要的更多:一棵树状的JSON串

如上的表生成如下的树:

[
    {
        "id": "1",
        "text": "总工程1",
        "items": [
            {
                "id": "2",
                "text": "单位工程1",
                "items": [
                    {
                        "id": "4",
                        "text": "分部工程1",
                        "items": [
                            {
                                "id": "6",
                                "text": "分项工程1",
                                "items": []
                            },
                            {
                                "id": "8",
                                "text": "分项工程3",
                                "items": []
                            }
                        ]
                    },
                    {
                        "id": "9",
                        "text": "分部工程3",
                        "items": []
                    }
                ]
            },
            {
                "id": "3",
                "text": "单位工程2",
                "items": [
                    {
                        "id": "5",
                        "text": "分部工程2",
                        "items": []
                    }
                ]
            }
        ]
    },
    {
        "id": "7",
        "text": "总工程2",
        "items": []
    }
]

说明:表中的数据理论上可能是无限级的

方案1: 在数据库中生成.各种测试,在STACKOVERFLOW 上也找, 但方案都是针对某一个层的,

   或是具体某些数据, 花了两到三天,最终放弃!

方案2: 当然就是生成TABLE, 在C# 里拼接, 看起来不是那么的难, 至少比POSTGRES 简单

           可是事实上, 并不容易(对我来说)

     最终写了一个很烂很烂的类吧, 如下:

public class jsonTree
{
    public jsonTree(DataTable dt)
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
        this._myDT = dt;
    }
    private DataTable _myDT = new DataTable();
    private string json = string.Empty;

    public  string getJSON()
    {
        int iRow = _myDT.Rows.Count;
        Int32 iLvl = 1;
        Int32 iMaxLvl = 0;
        Int32 iTemp = 0;
        for(int i=0;i<iRow;i++)
        {
            iTemp=Convert.ToInt32(_myDT.Rows[i]["level"].ToString());
            if (iTemp > iMaxLvl) iMaxLvl = iTemp;
        }
        getChild(0,iLvl,iMaxLvl);
        json = json.Substring(8,json.Length-8); //删除开头的 items:
        return json;
    }
    private void getChild( Int32 parentID,Int32 iLvl,Int32 iMaxLvl)
    {
        int iRows = _myDT.Rows.Count;
        json += "\"items\":[";
        for (int i = 0; i < iRows; i++)
        {
            if (iLvl > iMaxLvl)
            {
                break;
            }
            if (Convert.ToInt32(_myDT.Rows[i]["parent_id"].ToString()) == parentID && iLvl == Convert.ToUInt32(_myDT.Rows[i]["level"].ToString()))
            {
                json += "{\"id\":\"" + Convert.ToInt32(_myDT.Rows[i]["id"].ToString()) + "\",";
                json += "\"text\":\""+_myDT.Rows[i]["name"].ToString()+"\",";
                getChild(Convert.ToInt32(_myDT.Rows[i]["id"].ToString()),iLvl+1,iMaxLvl);
                json += "},";
            }
            if (i == iRows - 1 && iLvl < iMaxLvl)
            {
                    if(json.Substring(json.Length - 1, 1) != "[") 
                        json = json.Substring(0, json.Length - 1);
            } 
            if(iLvl==iMaxLvl  && json.Substring(json.Length - 1, 1) == "," && i==iRows-1)
                json = json.Substring(0, json.Length - 1);
        }
        json += "]";
    }
}

调用时:   1.    直接先生成一个表(select * from Table) myTable

     2.    jsonTree myTree = new jsonTree(myTable);

      string json = myTree.getJSON();

虽然不是很美,不管怎么样, 至少它工作了!

先放在这里, 大家可参考一下, 能有好的意见就更好了!

以后有机会再作修改!

-------------------------------------

这里有个小插曲:生成时  是这样的     items:[]

我返回时,JS怎么也不认!!!!

原因:          标准的JSON 是以 {}或[]开头的

   另:       JSON的KEY-VALUE都需要双引号 

 

posted @ 2017-09-01 11:53  ssqhan  阅读(597)  评论(0编辑  收藏  举报