需要引用Newtonsoft.Json.dll
private void button2_Click(object sender, EventArgs e)
{
//{\"id\":\"329\",\"txt\":\"IT\"}
string json = "[{\"Groupid\": \"1\",\"groupnum\": \"9005\",\"groupname\": \"调度中心\",\"type\": \"1\",\"dnsprefix\": \"\",\"islocal\": \"1\",\"canshowall\": \"0\",\"user\": [],\"group\": [{\"Groupid\": \"54\",\"groupnum\": \"66000\",\"groupname\": \"大唐移动\",\"type\": \"0\",\"dnsprefix\": \"\",\"islocal\": \"1\",\"canshowall\": \"1\",\"user\": [],\"group\": [{\"Groupid\": \"55\",\"groupnum\": \"67000\",\"groupname\": \"大唐移动1\",\"type\": \"1\",\"dnsprefix\": \"\",\"islocal\": \"1\",\"canshowall\": \"1\",\"user\": [],\"group\": []}] },{\"Groupid\": \"66\",\"groupnum\": \"66000\",\"groupname\": \"大唐联通\",\"type\": \"0\",\"dnsprefix\": \"\",\"islocal\": \"1\",\"canshowall\": \"1\",\"user\": [],\"group\": [{\"Groupid\": \"67\",\"groupnum\": \"67000\",\"groupname\": \"大唐联通1\",\"type\": \"1\",\"dnsprefix\": \"\",\"islocal\": \"1\",\"canshowall\": \"1\",\"user\": [],\"group\": []}] }]}]";
BindTreeView(treeView,json);
}
///
/// 绑定树形控件
///
///
///
public void BindTreeView(TreeView treeView, string strJson)
{
treeView.Nodes.Clear();
if (IsJOjbect(strJson))
{
JObject jo = (JObject)JsonConvert.DeserializeObject(strJson);
foreach (var item in jo)
{
TreeNode tree;
if (item.Value.GetType() == typeof(JObject))
{
tree = new TreeNode(item.Key);
AddTreeChildNode(ref tree, item.Value.ToString());
treeView.Nodes.Add(tree);
}
else if (item.Value.GetType() == typeof(JArray))
{
tree = new TreeNode(item.Key);
AddTreeChildNode(ref tree, item.Value.ToString());
treeView.Nodes.Add(tree);
}
else
{
tree = new TreeNode(item.Key + ":" + item.Value.ToString());
treeView.Nodes.Add(tree);
}
}
}
if (IsJArray(strJson))
{
JArray ja = (JArray)JsonConvert.DeserializeObject(strJson);
int i = 0;
foreach (JObject item in ja)
{
TreeNode tree = new TreeNode();
foreach (var itemOb in item)
{
TreeNode treeOb;
if (itemOb.Value.GetType() == typeof(JObject))
{
treeOb = new TreeNode(itemOb.Key);
AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
else if (itemOb.Value.GetType() == typeof(JArray))
{
treeOb = new TreeNode(itemOb.Key);
AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
else
{
treeOb = new TreeNode(itemOb.Key + ":" + itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
}
treeView.Nodes.Add(tree);
}
}
treeView.ExpandAll();
}
///
/// 添加子节点
///
///
///
public void AddTreeChildNode(ref TreeNode parantNode, string value)
{
if (IsJOjbect(value))
{
JObject jo = (JObject)JsonConvert.DeserializeObject(value);
foreach (var item in jo)
{
TreeNode tree;
if (item.Value.GetType() == typeof(JObject))
{
tree = new TreeNode(item.Key);
AddTreeChildNode(ref tree, item.Value.ToString());
parantNode.Nodes.Add(tree);
}
else if (item.Value.GetType() == typeof(JArray))
{
tree = new TreeNode(item.Key);
AddTreeChildNode(ref tree, item.Value.ToString());
parantNode.Nodes.Add(tree);
}
else
{
tree = new TreeNode(item.Key + ":" + item.Value.ToString());
parantNode.Nodes.Add(tree);
}
}
}
if (IsJArray(value))
{
JArray ja = (JArray)JsonConvert.DeserializeObject(value);
int i = 0;
foreach (JObject item in ja)
{
TreeNode tree = new TreeNode();
parantNode.Nodes.Add(tree);
foreach (var itemOb in item)
{
TreeNode treeOb;
if (itemOb.Value.GetType() == typeof(JObject))
{
treeOb = new TreeNode(itemOb.Key);
AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
else if (itemOb.Value.GetType() == typeof(JArray))
{
treeOb = new TreeNode(itemOb.Key);
AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
else
{
treeOb = new TreeNode(itemOb.Key + ":" + itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
}
}
}
}
///
/// 判断是否JOjbect类型
///
///
///
public bool IsJOjbect(string value)
{
try
{
JObject ja = JObject.Parse(value);
return true;
}
catch (Exception ex)
{
return false;
}
}
///
/// 判断是否JArray类型
///
///
///
public bool IsJArray(string value)
{
try
{
JArray ja = JArray.Parse(value);
return true;
}
catch (Exception ex)
{
return false;
}
}