我正在使用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
}]
}]
}
我在DataSet中的结果是:
文本可见叶展开
帐单0真的假
销售0真的假
设备1真假
过程1个真假
所有带有visible ='1'的记录应该在'Visible'节点下生成,所有带有visible ='0'的记录应该在'Invisible'节点下生成。我只是无法生成JSON。如何以上述格式生成它?
答案 0 :(得分:0)
我创建了一个类,如下所示:
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;