C#如何将datatable数据转换成json 30
varindexdata2=[{isexpand:'true',text:'根节点',children:[{isexpand:'true',text:"子节点",chil...
var indexdata2 =
[
{ isexpand: 'true', text: '根节点', children: [
{ isexpand: 'true', text: "子节点", children: [
{ url: 'a.aspx', text: '子节点1' },
{ url: 'b.aspx', text: '子节点2' },
{ url: 'c.aspx', text: '子节点3' }
]
}]
}
];
如上是我要转换的格式,我要查询菜单,先查出根节点,再根据根节点id查出相应子节点,依次查下去,得到的数据转成json,赋值给前台js变量。怎么做? 展开
[
{ isexpand: 'true', text: '根节点', children: [
{ isexpand: 'true', text: "子节点", children: [
{ url: 'a.aspx', text: '子节点1' },
{ url: 'b.aspx', text: '子节点2' },
{ url: 'c.aspx', text: '子节点3' }
]
}]
}
];
如上是我要转换的格式,我要查询菜单,先查出根节点,再根据根节点id查出相应子节点,依次查下去,得到的数据转成json,赋值给前台js变量。怎么做? 展开
2013-03-17 · 知道合伙人软件行家
关注
展开全部
/// <summary>
//橡让/ 将datatable转换为json
/// </summary>
/// <param name="纳桥dtb">Dt</param>
/// <returns>JSON字符串</returns>
public static string Dtb2Json(DataTable dtb)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
System.Collections.ArrayList dic = new System.Collections.ArrayList();
foreach (DataRow dr in dtb.Rows)
{
System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<梁茄局string, object>();
foreach (DataColumn dc in dtb.Columns)
{
drow.Add(dc.ColumnName, dr[dc.ColumnName]);
}
dic.Add(drow);
}
//序列化
return jss.Serialize(dic);
}
//橡让/ 将datatable转换为json
/// </summary>
/// <param name="纳桥dtb">Dt</param>
/// <returns>JSON字符串</returns>
public static string Dtb2Json(DataTable dtb)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
System.Collections.ArrayList dic = new System.Collections.ArrayList();
foreach (DataRow dr in dtb.Rows)
{
System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<梁茄局string, object>();
foreach (DataColumn dc in dtb.Columns)
{
drow.Add(dc.ColumnName, dr[dc.ColumnName]);
}
dic.Add(drow);
}
//序列化
return jss.Serialize(dic);
}
追问
没明白,
1. 我查询根节点保存到datatable1, 然后查询下级几点保存到datatable2 ....
2. 前台js变量定义的是 var indexdata2, 怎么把后台值传给indexdata2
???
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询