【问题标题】:Generating JSON from DataSet从数据集生成 JSON
【发布时间】:2013-12-18 15:39:38
【问题描述】:

我正在使用 ExtJS 生成一棵树。它需要以下格式的 JSON:

{  "children": [{
        "text": "Invisible",
        "expanded": true,
        "children": [{
            "text": "Billing",
            "leaf": true
        }, {
            "text": "Sales",
            "leaf": true
        }]
    },
    {
        "text": "Visible",
        "expanded": true,
        "children": [{
            "text": "Equipment",
            "leaf": true
        }, {
            "text": "Process",
            "leaf": true
        }]
    }]
}

我的数据集中的结果是:

文本                可见     叶子     展开
计费            0            真    假
销售           0                                                                                                                                                                 
设备    1             真    假
处理       1             真    假

所有可见='1'的记录都应该在'可见'节点下生成,所有可见='0'的记录都应该在'不可见'节点下生成。我只是无法生成 JSON。如何生成上述格式?

【问题讨论】:

标签: c# json extjs4


【解决方案1】:

我创建了一个类如下:

public class TreeNode
{
    public string text { get; set; }
    public bool leaf { get; set; }
    public bool expanded { get; set; }
    public List<TreeNode> children { get; set; }

    public TreeNode()
    {
        leaf = false;
        expanded = true;
        children = new List<TreeNode>();
    }
}

根据返回的数据集,我创建了如下对象:

List<TreeNode> treeNodeList = new List<TreeNode>();
            treeNodeList.Add(new TreeNode
            {
                text = "Invisible",
                expanded = true,
                leaf = false
            });
            treeNodeList.Add(new TreeNode
            {
                text = "Visible",
                expanded = true,
                leaf = false
            });

            if (ds.Tables.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    treeNodeList[0].children.Add(new TreeNode
                                        {
                                            text = ds.Tables[0].Rows[i][0].ToString(),
                                            leaf = true
                                        });
                }
            }

            if (ds.Tables.Count > 1)
            {
                for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                {
                    treeNodeList[1].children.Add(new TreeNode
                    {
                        text = ds.Tables[1].Rows[i][0].ToString(),
                        leaf = true
                    });
                }
            }
            return treeNodeList;

【讨论】:

    猜你喜欢
    • 2016-10-17
    • 2014-06-29
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 2022-01-17
    相关资源
    最近更新 更多